Kint
Kint

Reputation: 133

Getting attribute from class using javascript?

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>

Upvotes: 0

Views: 42

Answers (1)

Sjors
Sjors

Reputation: 1215

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'));

Upvotes: 2

Related Questions