Skip to content Skip to sidebar Skip to footer

Using A Variable In :contains - How To

I am using accordion menu on an e-commerce site. The menu stays open with the options until the product page. On that page I am trying to use the breadcrumbs to match text in the m

Solution 1:

Try:

$('ul#accordion a:contains(' + bctext + ')')

Solution 2:

You could use Traversing/contains also:

$('ul#accordion a').contains(bctext);

Solution 3:

Change:

$('ul#accordion a:contains(bctext)').parent().parent().addClass('special');

to:

$('ul#accordion a:contains(' + bctext + ')').parent().parent().addClass('special');

The first way, the way you had it, is telling JavaScript to use the string 'bctext' as the contains parameter. What you actually want is the string which is contained within the bctext variable. Therefore you have to use it outside of the quotes and use concatenation to create your selector string.

Solution 4:

$('ul#accordion a').filter(':contains(' + bctext + ')'

is now preferred to the deprecated

$('ul#accordion a').contains(bctext); 

(as commented by Crescent Fresh)

Solution 5:

This line should be:

$('ul#accordion a:contains(' + bctext + ')').parent().parent().addClass('special');

Post a Comment for "Using A Variable In :contains - How To"