I'm trying to get the src of a specific image on a web page. When I enter my code, the output is an undefined error.
Uncaught TypeError: Cannot read attribute of undefined (read 'getAttribute')
This is the HTML code
<div class="team-member-image border-radius-5px overflow-hidden"> <img class="abc" alt="" src="images/team01.jpg"> </div>
This is js
`<script> var pic = document.getElementsByClassName("abc")[0].getElementsByTagName("img")[0]; var src = pic.getAttribute('src'); console.log(src); </script>`
What could this problem be and how can I solve it.
P粉6656790532023-09-14 16:31:56
Just call it once to get the element:
var pic = document.getElementsByClassName("abc")[0];