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.
getElementsByClassName return a collection. So you just have to do this :
document.getElementsByClassName('logo')[0].getElementsByTagName('img')[0].src ="/b.jpg";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With