jack
jack

Reputation: 403

jquery - variable nth-child?

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

Answers (3)

Giles Smith
Giles Smith

Reputation: 1972

Try:

var position = 5;

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

Upvotes: 6

jAndy
jAndy

Reputation: 236202

You need quoting:

var position = 5;

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

Upvotes: 6

Sarfraz
Sarfraz

Reputation: 382909

Try:

var position = 5;

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

Upvotes: 41

Related Questions