I’m trying to get the src in a specific image on my webpage. When I entered my code the output is an undefined error.
Uncaught TypeError: Cannot read properties of undefined (reading ‘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 the js
`<script>
var pic = document.getElementsByClassName("abc")[0].getElementsByTagName("img")[0];
var src = pic.getAttribute('src');
console.log(src);
</script>`
What can be the issue in this and how can I solve this.
>Solution :
Just call once get element:
var pic = document.getElementsByClassName("abc")[0];