0

How would I get the attribute 'src' from the following tag:

<div class="entry"><img width="300" height="200" src="http://domain/myimg.jpg" alt="text" /></div>
2
  • 2
    Multiple ways of getting the src of that element. What have you tried so far? Can you give that element an ID? If so you can access the src by: document.getElementById('ImageID').src; Commented May 7, 2015 at 21:51
  • Do you have access to jquery? If so, just use $('div.entry img').attr('src') to get the source attribute. Commented May 7, 2015 at 22:00

1 Answer 1

2

I would recommend using an ID for the 'entry' element (or directly on the image element) instead of a class.

If that's not possible use this:

var x = document.querySelector('.entry img');
alert('src: ' + x.getAttribute('src'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.