0

I have created a nav menu with bootstrap nav. I have added a small jquery code when i press mobile toggle menu it will close automatically and go destination

but if i have drop down menu in nav. this drop menu now not working. when i press dropdown menu parent for expand its close the menu automatically. and i cant check which menu available in drop down menu so i have tried to add a condition. but its not working.

in dropdown menu parent have class called .dropdown-toggle. if this class available it shouldn't be close automatically

if (jQuery(".navbar-collapse a").val() !== '.dropdown-toggle') {
    jQuery('.navbar-collapse a').click(function (e) {
        jQuery('.navbar-collapse').collapse('toggle');
    });
}

And here the html code

<ul class="nav navbar-nav">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
          </ul>
        </li>
      </ul>

1 Answer 1

0

Try this, your parent menu item will work and as well as mobile menu toggle

 <script>
 (function($) {
 jQuery(function($) {
 $('.navbar .dropdown').click(function() {
  $(this).find('.dropdown-menu').first().stop(true, true).slideToggle();
  }, function() {
  $(this).find('.dropdown-menu').first().stop(true, true).slideUp();
  });
  $('.navbar .dropdown > a').click(function() {   

    location.href = this.href;
  });
  });
  })(jQuery);
  </script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.