javascript - Close other menus on page before new one opens -


$(function () {     var pull_1 = $('#pull-main-menu');     var menu_1 = $('#mobile-menu');      pull_1.on('click', function (e) {         e.preventdeenter code herefault()         `enter code here`;         menu_1.slidetoggle();     }); });  $(function () {     var pull_2 = $('#pull-first-menu');     var menu_2 = $('#first-menu-top-left');      pull_2.on('click', function (e) {         e.preventdefault();         menu_2.slidetoggle();     }); });  $(function () {     var pull_3 = $('#pull-second-menu');     var menu_3 = $('#welcome');      pull_3.on('click', function (e) {         e.preventdefault();         menu_3.slidetoggle();     }); }); 

html:

<a href="#" id="pull-main-menu"></a>     <div id="mobile-menu">         <ul>             <li><a href=""></a></li>             <li><a href=""></a></li>         </ul>     </div>    <a href="#" id="pull-first-menu"></a>     <ul id="first-menu-top-left">         <li><a href=""></a></li>         <li><a href=""></a></li>     <ul> 

i have 3 functions open 3 different menus on page. can't figure out how write conditions these functions close opened menu before new 1 gets opened.

you can alter html little use classes, , combine functions one. run snippet below see work.

$(document).ready(function(){      $('.ddtoggle').click(function(e){        $(this).next('.ddmenu').slidetoggle();          $('.ddmenu').not($(this).next('.ddmenu')).hide()      }).next('.ddmenu').hide();  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>    <a href="#" class="ddtoggle" id="pull-main-menu">menu 1</a>  <div id="mobile-menu"  class="ddmenu">      <ul>          <li><a href="">menu item 1</a>          </li>          <li><a href="">menu item 1</a>          </li>      </ul>  </div>     <a href="#" class="ddtoggle" id="pull-first-menu">menu 2</a>  <ul id="first-menu-top-left" class="ddmenu">      <li><a href="">menu item 2</a>      </li>      <li><a href="">menu item 2</a>      </li>  <ul>


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -