Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementsByTagName is not a function [duplicate]

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.

like image 311
dcs Avatar asked Apr 29 '15 12:04

dcs


1 Answers

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

document.getElementsByClassName('logo')[0].getElementsByTagName('img')[0].src ="/b.jpg";
like image 159
Anthony Granger Avatar answered Sep 24 '22 05:09

Anthony Granger