1

Best explained with code:

$(".myParent .myChild:nth-child(3n)").css('border-top-color','#ffffff');

√ Works

myVar = "3n";
$(".myParent .myChild:nth-child(myVar)").css('border-top-color','#ffffff');

X Doesn't work

This is obviously jQuery programming 101... but seriously, why on earth won't that work?! I'm passing on the same thing!

I tried it as > myVar = 3n (no string), obviously that shouldn't work, and it didn't.

1
  • 2
    “jQuery Programming 101” — more like JavaScript 101. Commented Mar 28, 2011 at 12:55

1 Answer 1

5

you var has to be concatenated

 var myVar = "3n";

 $(".myParent .myChild:nth-child("+myVar+")").css('border-top-color','#ffffff');
Sign up to request clarification or add additional context in comments.

2 Comments

Ah OK... so it's looking at the css as a string already so I have to go out the string to pick up the variable. Thanks a million.
@RGBK Yes but I wouldn't call it CSS to the right of the dot is CSS to the left is your jQuery selector 8 ^ )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.