Get the "alt" attribute from an image that resides inside an <a> tag

Viewed 72776

this should be fairly simple but for some reason, it doesn't work.

I want to get the "alt" attribute from an image that resides inside an tag

<div id="album-artwork">
<ul>
   <li><a href="link to large image"><img src="thumbnail" alt="description of the image" /></a></li>
   ...
</ul>
</div>



$("#album-artwork a").click(function(e) {
e.preventDefault();

var src = $(this).attr("href");
var alt = $(this).next("img").attr("alt");


alert(src); // ok!
alert(alt); //output: undefined

});

any ideas why next() doesn't work? Is there a better solution out there?

Thanks in advance Marco

8 Answers

Dont complicate:

$('img').on({'click': function(){
    var alt = this.alt;         
Related