Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  67] [ 1]  / answers: 1 / hits: 28807  / 9 Years ago, wed, april 29, 2015, 12:00:00

I am really struggling this. I am wanting to change the src attribute of an img tag and get the error message getElementsByTagName is not a function. The following is my test markup



<html>
<body>
<div class=logo>
<img src=/a.jpg>
</div>
<script>
document.getElementsByClassName('logo').getElementsByTagName('img')[0].src =/b.jpg;
</script>
</body>
</html>


Any advice is appreciated.


More From » html

 Answers
8

getElementsByClassName return a collection.
So you just have to do this :



document.getElementsByClassName('logo')[0].getElementsByTagName('img')[0].src =/b.jpg;

[#66828] Tuesday, April 28, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;