search

Home  >  Q&A  >  body text

The rephrased title is: Extract the original file of the image

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粉087951442P粉087951442456 days ago530

reply all(1)I'll reply

  • P粉665679053

    P粉6656790532023-09-14 16:31:56

    Just call it once to get the element:

    var pic = document.getElementsByClassName("abc")[0];

    reply
    0
  • Cancelreply