views:

28

answers:

3

Hey. I want to pass a variable as an argument into an nth-child selector.
This doesn't work:

var position = 5;

$("#daddy > div:nth-child(position)").animate({
    opacity: 0.01,
}, 500);​

Is it possible though? Cheers, Jack

+3  A: 

Try:

var position = 5;

$("#daddy > div:nth-child(" + position + ")").animate({
    opacity: 0.01,
}, 500);​
Sarfraz
nailed it! thanking you....
jack
@jack: Welcome :)
Sarfraz
+1  A: 

You need quoting:

var position = 5;

$("#daddy > div:nth-child(" + position + ")").animate({
   opacity: 0.01,
}, 500);​
jAndy
+1  A: 

Try:

var position = 5;

$("#daddy > div:nth-child(" + position + ")").animate({
    opacity: 0.01,
}, 500);​
Giles