-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,065
  • 5
  • 50
  • 81

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
  • 41,872
  • 14
  • 128
  • 201