5

I have some text from a breadcrumb which I am using to open menu items on a page. For example, say the bctext = 'pasta'.

I want to target the word "pasta", but not say "yadda yadda yadda pasta". Only an instance of the single word "pasta" should match, or if bctext were a phrase, then it would only find the exact phrase.

This is what I have so far:

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

But this finds "yadda yadda pasta", of course.

I get the bctext with the following:

var bctext = $('#CategoryBreadcrumb ul li:last-child').prev().children().text();

Then, I edit the menu with the following:

$('ul#accordion a:contains(' + bctext + ')').parent()
                                            .addClass('special')
                                            .children('ul')
                                            .css('display','block'); 

Is what I'm going for possible?

1 Answer 1

14
$('ul#accordion a').filter(function() {
    return $(this).text() == bctext;
}).parent().addClass('special').children('ul').css('display','block');

:contains() is not a native selector anyway so using .filter() with a custom callback won't have any performance drawbacks.

3
  • so sorry my jquery is not great... i am going to add more of my code above because i am not sure how to work this into what i did.
    – liz
    Commented Apr 30, 2012 at 17:44
  • so above i added what the full code is. so now just to be clear. (and maybe i have this all wrong...) i use the filter function to find the text in the a tag and give it a variable and then compare that to the bctext i found in the breadcrumb?
    – liz
    Commented Apr 30, 2012 at 17:48
  • ahh i didnt know you could chain onto a function like that!
    – liz
    Commented Apr 30, 2012 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.