Why is it that using variable within :contains() doesn't work? Is there any way to make it work? filter('contains()')
didn't work either.
<h2>Test</h2>
<div>Test</div>
var model = $('h2').text();
//variable doesn't work
$('div:contains(model)').css('background','yellow');
//obviously, string works
$('div:contains("Test")').css('background','yellow');
Selectors doesn't use Javascript variables. You have to put the value from the variable in the selector string:
$('div:contains("' + $modele + '")')
Unlike PHP, Javascript does not support string interpolation.
If you want to use a variable inside a string, you must concatenate it using the +
operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With