jquery :contains selector using variable

contains, jquery

Solution

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.

Problem

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'); ```

Original source

Related problems