-8

This might be a really silly question, but I have this CSS selector:

$('.result:nth-child(25) .name .title')

Which works. But when I set a variable a = 25, it breaks

$('.result:nth-child(a) .name .title')

 Uncaught Error: Syntax error, unrecognized expression: :nth-child

What am I doing wrong?

Morgan Allen
  • 3,291
  • 8
  • 62
  • 86

1 Answers1

4

You'll have to insert the value into the string manually in JavaScript using string concatenation. Try this instead:

$('.result:nth-child(' + a + ') .name .title')
Alexis King
  • 43,109
  • 15
  • 131
  • 205