0

I've got this code in jquery

$('.candidate-single:has(p:contains("Norfolk"))').addClass('is-hidden');

In place of where it says Norfolk I want to use a variable but can't seem to get it to work. I take it i need to escape the string somehow but can't quite work out how.

Thank you!

Luke Guy
  • 35
  • 4

3 Answers3

0

if you declare your variable :

var string = "Norflock"

you can use your code as it :

$('.candidate-single:has(p:contains("' + string + '"))').addClass('is-hidden');
Stéphane Ammar
  • 1,414
  • 8
  • 16
0

Just break out of your single quotes:

$('.candidate-single:has(p:contains("' + variable + '"))').addClass('is-hidden');

delinear
  • 2,575
  • 1
  • 8
  • 20
0

If you want to use variable instead of direct string then you can do in this way

var searchvariable=="Norfolk";
$('.candidate-single:has(p:contains("'+searchvariable+'"))').addClass('is-hidden');

Please share more detail if this is not helpful.

dipmala
  • 1,961
  • 1
  • 15
  • 17