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
  • 35
  • 5

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,454
  • 10
  • 17
0

Just break out of your single quotes:

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

delinear
  • 2,745
  • 1
  • 10
  • 21
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
  • 2,003
  • 1
  • 16
  • 17