0

I need to add the class .active-trail to an li element if the li element has a link (a) as a child element containing the word "Help".

Example:

<li class="last expanded dropdown">

  <a href="/test1" title="" class="dropdown-toggle" data-target="#" data toggle="dropdown">
    Help <span class="caret"></span>
  </a>

</li>

<li class="last expanded dropdown">

  <a href="/test1" title="" class="dropdown-toggle" data-target="#" data toggle="dropdown">
    Contact <span class="caret"></span>
  </a>

</li>

In the example above, I would like to add the class active-trail to the first li like so:

<li class="last expanded dropdown active-trail">

  <a href="/test1" title="" class="dropdown-toggle" data-target="#" data toggle="dropdown">
    Help <span class="caret"></span>
  </a>

</li>

<li class="last expanded dropdown">

  <a href="/test1" title="" class="dropdown-toggle" data-target="#" data toggle="dropdown">
    Contact <span class="caret"></span>
  </a>

</li>

Is this possible through javascript/jQuery, and if so, how? Thanks!

3 Answers 3

2

Like this -

$('a').filter(function(){
  return $(this).text().indexOf('Help') > -1;
}).closest('li').addClass('active-trail');
Sign up to request clarification or add additional context in comments.

Comments

0
$('li a').each(function() {
    if( $(this).text().toLowerCase().search('Help') >= 0 ) {
        $(this).closest('li').addClass('active-trail')
    }
})

Edit: I like Mohammad's solution better, but this should work too.

Comments

0
$.each($('li a'),function() {
    if ($(this).text.indexOf('Help') > -1) {
        $(this).parent().addClass('active-trail');
    }
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.