jQuery open and close nested ul nav depending on location

Posted by firefusion on Programmers See other posts from Programmers or by firefusion
Published on 2011-01-11T10:54:33Z Indexed on 2011/01/11 10:59 UTC
Read the original article Hit count: 244

Filed under:

I'm making a sub nav in wordpress and have a nested list style menu. An example of the HTML is below. Whichever is the current item has the li class "current_page_item". I need all child menus collapsed unless there is a current_page_item class on the parent or one of the children.

<ul>
 <li class="current_page_item"><a href="#">Parent Item</a>
  <ul class="children">
   <li><a href="#">Child page</a></li>
   <li><a href="#">Child page</a></li>
   <li><a href="#">Child page</a></li>
   <li><a href="#">Child page</a></li>
  </ul>
 </li>
 <li><a href="#">Parent Item</a>
  <ul class="children">
   <li><a href="#">Child page</a></li>
   <li><a href="#">Child page</a></li>
  </ul>
 </li>
 <li><a href="#">Parent Item</a>
  <ul class="children">
   <li><a href="#">Child page</a></li>
   <li><a href="#">Child page</a></li>
  </ul>
 </li>
 <li><a href="#">Parent Item</a></li>
 <li><a href="#">Parent Item</a></li>
</ul>

This so far, which works but i wonder if it can be improved as there is some flashing open and then closed again....

jQuery('ul.children').slideUp(); jQuery('li.current_page_item ul.children').slideDown('medium'); jQuery('li.current_page_item').parent().slideDown('medium');

© Programmers or respective owner

Related posts about jQuery