Amit
Amit

Reputation: 26336

jquery nth-child issue

My code is like this:

var childNo = 2;
$('#someSelector tr:nth-child(childNo) td:first span').addClass('font-strike');

but it doesn't add the class. If i replace childNo with 2 it works fine. any idea how i can get it to work?

Upvotes: 0

Views: 111

Answers (3)

xanadont
xanadont

Reputation: 7614

$('#someSelector tr:nth-child(' + childNo + ' ) td:first span').addClass('font-strike');

Upvotes: 0

Haim Evgi
Haim Evgi

Reputation: 125674

you need to wrap your variable like :

$('#someSelector tr:nth-child('+childNo'+) td:first span').addClass('font-strike');

Upvotes: 0

nathan gonzalez
nathan gonzalez

Reputation: 12017

you're putting the string literal 'childNo' into your selector. replace it with:

$('#someSelector tr:nth-child('+childNo+') td:first span').addClass('font-strike');

and it should work

Upvotes: 1

Related Questions