jquery - When one dropdown is opened close another -
i have 2 drop-down menus mobile responsive site, 1 blog categories , drop-down search box. these work fine when open one, can little messy. have been looking when 1 open , user clicks open another, listen out , close it. have had research , have found functions use 'parent' , 'child' not sure how can apply snippet of code. have looked removing classes nothing seems trick. if has suggestions problem i'd grateful.
html
<div id="nav-mobile-responsive"><!--mobile navigation--> <div class="categories-mobile-responsive"> <ul id="nav-1"> <li> <h3></h3> <ul> <?php wp_list_categories('&title_li=') ?> </ul> </li> </ul> </div> <div class="searchbar-mobile-responsive"> <ul id="nav-2"> <li><h3></h3> <ul> <form method="get" id="searchform-mobile" action="<?php echo home_url( '/' ); ?>"> <div id="search-inputs"> <input type="text" value="" name="s" id="search-box" placeholder="search" /> <input type="hidden" name="ref_url" value="<?php esc_url($_server['request_uri'])?>"> </div> </form> </ul> </li> </ul> </div> </div>
javascript toggle both drop-downs mobile , small screens:
$('.categories-mobile-responsive,.searchbar-mobile-responsive').click(function(){ $(this).find('ul#nav-1,ul#nav-2').find('ul').slidetoggle(); }); $("#searchform-mobile").click(function(event){ event.stoppropagation(); });
before $(this).find('ul#nav-1,ul#nav-2').find('ul').slidetoggle();
, slideup other drop down menus.
$('.categories-mobile-responsive,.searchbar-mobile-responsive').click(function(){ $(this).siblings().find('ul ul').slideup(); $(this).find('ul#nav-1,ul#nav-2').find('ul').slidetoggle(); });
extra: bits of code unnecessary , can optimized (unless havn't provided entire html structure - in case may break it):
$('nav-mobile-responsive div').click(function(){ //only 1 selector $(this).siblings().find('ul ul').slideup(); $(this).find('ul ul').slidetoggle(); //only call .find() once });
source(s)
Comments
Post a Comment