A Happy Primary Menu Is An Active Primary Menu

10 Mar in css, drupal, jQuery
article image

I was hard at work on a recent website when I came across an interesting issue, you see I wanted to use a primary menu at the top as main navigation for the website. No dropdowns or anything fancy, just a smooth top horizontal navigation bar.

Additionally, I have a different menu (could one of several options) displayed along the right side based on the content being shown. Now I want my primary menu to display as 'active' when I am somewhere deep within the site on a page that the primary menu has no idea about. So, I want my primary navigation menu item to be active, even tho it has no idea what page I am on.

Let me paint a picture for you...

The primary navigation would be Games, Books, DVD's across the top.
The menu (not sub menu of primary navigation, but a sperate menu) for Books would include Writers, Covers, Fiction, Non-Fiction.
The menu for Games would include PSP, DS, Xbox.
and so on

More example...

When a user is on a page/node/view that pertains to a Book, the right side menu would be the 'Books Menu Block'. By using what drupal gives you within blocks (visibility settings), you can display the block when a provided url is present. So //sitename.com/books/cool-list, and //sitename.com/books/scary would enable drupal to show the 'Books Menu Block'. You can standardize your urls with pathauto, and use a good naming convention for your views and panels to accomplish this task. You can imagine that the same process can be build for the other types of content and their corresponding menus.

Are you still with me... good...

My problem was to figure out how to keep the primary menu link "active" when I was displaying content that was related to that link. There are probably a dozen ways to do it, but I really wanted to try some jQuery, and I really wanted to keep it simple. So here is what I decided to do.

I am good enough with jQuery and css that I decided to use the id and class selectors.
I created the above functionality for my menus, so when ever content was being displayed the menu was going to be associated with the url.
So, my goal was to get to the primary menu, by way of the menu being displayed on the right.

I checked to see if the menu was present using the menu's ID.

$('#this-is-the-css-id-of-the-block')[0])

If the menu was present I wanted to add a class of 'active' to the appropriate primary navigation.

$('li.class-name a').addClass('active')

$('li.class-name').addClass('active')

Then use as many if statements as I needed to accomplish my task. My final jQuery looked something like this...

$(document).ready(function() {
  if ($('#block-menu-menu-one')[0]) {
    $('li.menu-111 a').addClass('active');
    $('li.menu-111').addClass('active');
  }
  if ($('#block-menu-menu-two')[0]) {
    $('li.menu-112 a').addClass('active');
    $('li.menu-112').addClass('active');
  }
  if ($('#block-menu-menu-three')[0]) {
    $('li.menu-113 a').addClass('active');
    $('li.menu-113').addClass('active');
  }
  if ($('#block-menu-menu-four')[0]) {
    $('li.menu-114 a').addClass('active');
    $('li.menu-114').addClass('active');
  }
});

There are probably some better ways to write this, but for my current jQuery skills I was happy with the results. If you need some ideas about creating some custom li classes you can always look at the first blogthingee post.

Comments

I tried it

Alan, I tried this module, but it didn't seem to work so well for me, perhaps because of the views, nodes, and such that I was using... and maybe I'm a little impatient :)

Thanks for the comment Alan, hopefully having the link here will help a ton of folks.

:)
Sean

wedding dresses

Wedding Dresses Evening Dresses Bridal Gowns Bridesmaid Dresses Prom Dresses Flower girl Dresses plus size wedding dress A-Line Wedding Dresses Ball Gown Wedding Dresses Fishtail Dresses Simple Style Wedding Dresses A Line Evening Dresses Wedding Accessories Ball Gown Bridesmaid Dresses A line Bridesmaid Dresses Wedding Veil Wedding Gloves weddding shoes Wedding tiaras

PHP, and no IDs

an age-old issue, afaik!

i would definitely look for a way to do this w/o needing to explicitly specify menu or block IDs, which will be a maintenance headache.

and another approach is to write some simple code to do the work on the server side, which seems preferable to relying on client-side scripting. here's my take on that:
http://drupal.org/node/334624#comment-1238021

-- andy

PHP vs jQuery

Great info, thanks Andy.

suv

A Happy Primary Menu Is An Active Primary Menu,If you are happy, you are more willing to do thingsappliance repair|free ads |employment

It worked like a charm.

After looking through a dozen or so Google results I came across this post, and with a little effort... it worked like a charm.

Happy Primary

where a single letter denotes three beats of that chord and the ones followed by full stops mean there are different chords to each beat of THAT bar. disney restaurants

please tell me where to put

please tell me where to put that jquery code snipet

whoesale christian louboutin in low price

Brilliant article! Your topic gives me a good advice.

shower enclosures Nicely

shower enclosures Nicely written! Very humourous, I liked it!

fashion

We will bring you more exciting and better service. Our spiderman costumes are fashionable and creative. Is even more affordable. we provide high quality catsuits.Look here, there is a very innovative style models, our zentai for sale We are waiting for you.. Please trust us. Choose us!http://zentaibodysuit.luxuryshoesmart.com

billiards supplies

They have create out more than one models in this year, that's a great move as a new comer. I think they will be famous easily. billiards supplies

Women's blouses

Mltailor, the chinese dress clothing store. there oline selling women,men,girls,ladies and kids' clothes. there are the custom design dresses,Chinese dress Qipao Cheongsam and clothing.

Post new comment