lundi 16 février 2015

Get src and set as variable via class (id not available)


I'm trying to get the URL of a certain image out of the given HTML code



<div class="image_div_name">
<img src="http://ift.tt/1Ei881f" alt="">
</div>


So this is my currect code that should solve it:



var picture = document.getElementsByClassName("image_div_name");
var src = picture[0];


If I now print picture via console.log, I get the correct div:



<div class="image_div_name">
<img src="http://ift.tt/1Ei881f" alt="">
</div>


However, if I want to access the src, it doesnt seem to work:



var src = picture[0].src;
console.log(src);


Gives back:



undedfined


If I try:



console.log(picture[0].innerHTML)


it gets better and I receive:



<img src="http://ift.tt/1Ei881f" alt="">


However I'm looking for way to merely get the URL itself, as in:



http://ift.tt/1Ei881f


Can you help? What am I doing wrong?





Aucun commentaire:

Enregistrer un commentaire