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


this should simple reason, doesn't work.

i want "alt" attribute image resides inside tag

<div id="album-artwork"> <ul>    <li><a href="link large image"><img src="thumbnail" alt="description of 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? there better solution out there?

thanks in advance marco

it's because image tag inside ul tag. next looks tags @ same level. you'll have use

var alt = $(this).children("img").attr("alt"); 

edit: included additional " missing


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -