Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retrieve the src from a image

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading