vendredi 28 mars 2014

Javascript get .src from specific image onclick


Vote count:

0




I'm trying to make an image swapper (click smaller image and it changes the image in a larger area of the screen). But I am getting the variable imgSrc as undefined. I believe this is because I am not getting the source for the specific image being clicked.



var imgSrc = document.getElementsByClassName('avatar-small').src;
console.log("img Src = " + imgSrc);


HTML:



<img class="avatar-small" onclick="selectAvatar()" src="images/avatars/1.png">
<img class="avatar-small" onclick="selectAvatar()" src="images/avatars/2.png">
<img class="avatar-small" onclick="selectAvatar()" src="images/avatars/3.png">


How can I make use of something using .this so I am actually getting the source from the image that is clicked?


The console log outputs:



img Src = undefined


asked 49 secs ago






Aucun commentaire:

Enregistrer un commentaire