Jarrett Onions Jarrett Onions - 3 years ago 130
jQuery Question

For loop index source selector

Any ideas why the code returns the

src
when outside the
for
loop, but inside it returns
undefined
for each loop?

var deckCount = $(".deck").children().length;
var findCard = $(".deck > .card:eq(1) > img").attr("src");
console.log("findCard eq1: " , findCard);

for(i=0 ; i < deckCount; i++)
{
var findCard = $(".deck > .card:eq(i) > img").attr("src");
console.log("findCard i: " , findCard);
}


View here:
www.jarrettonions.co.za
the code is part of the next button in the image gallery.

KAD KAD
Answer Source

You have to pass the value of i from the variable as follows:

var findCard = $(".deck > .card:eq("+i+") > img").attr("src");

In your case the i is within the selector and it is prohibiting the selection from happening since it is an invalid attribute to the eq()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download