jQuery auto increment for class name I'm trying to make a drop down menu with click event. I have this HTML structure:

&lt;ul id="menubar"&gt;
        &lt;li class="menu1"&gt;
            &lt;a href="#"&gt;Menu 1&lt;/a&gt;
            &lt;ul class="submenubar"&gt;
                &lt;li class="submenu1"&gt;&lt;a href="#"&gt;Submenu 1&lt;/a&gt;&lt;/li&gt;
                &lt;li class="submenu1"&gt;&lt;a href="#"&gt;Submenu 1&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li class="menu2"&gt;
            &lt;a href="#"&gt;Menu 2&lt;/a&gt;
            &lt;ul class="submenubar"&gt;
                &lt;li class="submenu2"&gt;&lt;a href="#"&gt;Submenu 2&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li class="menu3"&gt;
            &lt;a href="#"&gt;Menu 3&lt;/a&gt;
            &lt;ul class="submenubar"&gt;
                &lt;li class="submenu3"&gt;&lt;a href="#"&gt;Submenu 3&lt;/a&gt;&lt;/li&gt;
                &lt;li class="submenu3"&gt;&lt;a href="#"&gt;Submenu 3&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
&lt;/ul&gt;​


I want to avoid the double drop down, so I made this script:

$(document).ready(function(){
            $('li.submenu1').hide();
            $('li.menu1').click(function(e){
                $(this).find('li.submenu1', this).slideToggle('fast'); 
                $('li.submenu2').hide();     
                $('li.submenu3').hide();                         
                e.stopPropagation();
            })
            $('li.submenu2').hide();
            $('li.menu2').click(function(e){
                $(this).find('li.submenu2', this).slideToggle('fast');
                $('li.submenu1').hide();      
                $('li.submenu3').hide();                       
                e.stopPropagation();
            })  
            $('li.submenu3').hide();
            $('li.menu3').click(function(e){
                $(this).find('li.submenu3', this).slideToggle('fast');
                $('li.submenu1').hide();      
                $('li.submenu2').hide();                       
                e.stopPropagation();
            })
        })


Is there any way to simplify the code? Perhaps some auto-increment tricks will help.
Probably something like this would enable you to extract it. Basically I'd suggest looping through hiding everything then setting the one you want to open, you could be clever and check if the menu selected is already one, but hopefully this will give you the idea:

$("menubar ul").each(function(){
   $("li", this).click({
      $("menubar ul").each(function(){
         $("li ul", this).hide()
      })
      $("ul",this).toggle(...
   })
})