0

This is such a pity D7 does not have by default active links sets in the menu system. I mean everyone likes to have a clue where he is in the site by the active state of the menu.

I wonder why there is no such default feature in Drupal7?

Now I tried to do this in jQuery like this (see below) but it only works for predictable URL like static pages (it doesn't work for menu links created out of taxonomy terms). If someone can help me make it work for all the links in the menu I would appreciate.

  <script type="text/javascript">
  /*Script for creating active links*/
  $(document).ready(function(){
   var currentURL = document.location.pathname;
   var activeTopLevelPageArray = currentURL.split('/');
   var activeTopLevelPage = activeTopLevelPageArray[1];

   // applying the class to what we find
   $('#header ul li a[href^=/'+activeTopLevelPage+']')
    .addClass('active');
   // now making the nav menu do what we want with styles for the active element
    $('#header #primary-menu ul li a.active:first')
    .addClass('current-main') // assign main active bg to active link
    .parent('li') // select the parent list item
      .addClass('active-trail'); // then assign a class to it for manipulation

    /* If there is no match, just activate the "home" menu item/tab */
    var isActive = $('#header #primary-menu li.active-trail').size();
    if (isActive < 1) {
    $('#header #primary-menu li:first').addClass('active-trail');
    }
});
  </script>
0

I'm pretty sure Drupal 7 does that for you out of the box. I can see, from my D7 site, I have first, last active-trail classes

<ul class="menu"><li class="first leaf"><a href="/opportunities" title="">Opportunities</a></li>
<li class="leaf active-trail"><a href="/organisations" title="" class="active-trail active">Organisations</a></li>
<li class="last leaf"><a href="/events" title="">Events</a></li>
</ul>

As can be seen above the LI have the class you require. Do you have any custom code, modules or theme functions causing this not to happen on your site?

  • As above, there's default functionality for this. There's also other modules that extend this, the only use case I've come across that needs any scripting for this is when there are ajax facets and you want the navigation to change based on those. – Daniel Waters Sep 17 '13 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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