Reputation: 403
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
Upvotes: 19
Views: 36436
Reputation: 1972
Try:
var position = 5;
$("#daddy > div:nth-child(" + position + ")").animate({
opacity: 0.01,
}, 500);
Upvotes: 6
Reputation: 236202
You need quoting:
var position = 5;
$("#daddy > div:nth-child(" + position + ")").animate({
opacity: 0.01,
}, 500);
Upvotes: 6
Reputation: 382909
Try:
var position = 5;
$("#daddy > div:nth-child(" + position + ")").animate({
opacity: 0.01,
}, 500);
Upvotes: 41