jQuery variable iteration with .each()

Posted by user1143357 on Stack Overflow See other posts from Stack Overflow or by user1143357
Published on 2012-10-03T15:35:57Z Indexed on 2012/10/03 15:37 UTC
Read the original article Hit count: 114

Filed under:
|

My code needs to capture the 'src' of an image in a variable. It then needs to insert this variable into a 'href' tag on a link which surrounds the image.

My code is as follows:

$('.fancybox-image-li img').each(function(test) {

var test = $(this).attr("src");

$('.fancybox-image-link').attr('href', ''+test+'');

});

Unfortunately what seems to be happening is the 'test' var is only getting the attribute of the last img and then inserting this into all the 'href' attributes.

Any ideas how I can get the variable to change per image.

Cheers!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-selectors