Drupal: Expand / collapse all dhtml menu items

Like dhtml menus in Drupal? Then you’ll love this.
Two jquery functions that expand or collapse all dhtml menu items in Drupal in link/bookmarklet format:

javascript:$('.collapsed').each( function(){Drupal.dhtmlMenu.toggleMenu($(this));});void("Open all");
javascript:$('.expanded').each( function(){Drupal.dhtmlMenu.toggleMenu($(this));});void("Close all");

Drag this to your bookmark tool bar and enjoy menu control like never before:
Expand all menus
Collapse all menus

Back story:
I was toying with javascript today. The goal was to act on all menu items at once.
It occurred to me that the function already exists for each menu item so I just need to loop through them all.
jQuery to the rescue. You can loop through a collection of items with .each().

So our collection is all the elements with the class collapsed or expanded. Those are all <li>’s and we apply the function: Drupal.dhtmlMenu.toggleMenu to each. Hooray!

Leave a Comment