Chris
Chris

Reputation: 313

How to stop sub menu link clicking through and display sub nav

When i click my on navigation link to display the sub-menu its clicking through to the parent page.

How can i disable it clicking through if the link has a sub-menu, as the sub-menu displays briefly before the parent page loads.

$(function(){

   //Hide all the sub menus
   $('.sub-menu').hide();

   $("li:has(ul)").click(function(){
      //Find the child ul and slideToggle
      $(this).children("ul").slideToggle();
   });
});

<div class="main-nav navbar-right"><ul id="menu-main-navigation" class="main-nav-ul"><li id="menu-item-46" class="dropdown menu-item menu-item-type-post_type menu-item-object-page menu-item-home menu-item-46"><a href="http://www.chris-whiting.co.uk/development/">Home</a></li>
<li id="menu-item-77" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-77"><a href="http://www.chris-whiting.co.uk/development/about/">About</a></li>
<li id="menu-item-82" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-78 current_page_item menu-item-has-children menu-item-82"><a href="http://www.chris-whiting.co.uk/development/services/">Services</a>
<ul class="sub-menu" style="display: none;">
    <li id="menu-item-83" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-83"><a href="http://www.chris-whiting.co.uk/development/services/service-one/">Service One</a></li>
</ul>
</li>
<li id="menu-item-67" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-67"><a href="http://www.chris-whiting.co.uk/development/contact/">Contact</a></li>
</ul></div>

Upvotes: 1

Views: 626

Answers (1)

yaakov
yaakov

Reputation: 4655

Use preventDefault():

$("li:has(ul)").click(function(e){ // pass the click event to the function
      e.preventDefault(); // Stop the link from following
      //Find the child ul and slideToggle
      $(this).children("ul").slideToggle();
   });

Upvotes: 3

Related Questions