I have a menu list where some menu items also have sub-menus, with following structure:
<div>
<ul class="mobile-menu">
<li class="menu-item"><a href=..></a></li> // menu item without sub-menu
<li class="menu-item-has-children"> //menu item with sub-menu
<a href=...</a> //link attached to parent menu item
<ul class="sub-menu">
<li ><a href=...</a></li>
<li ><a href=...</a></li>
</ul>
</li>
</ul>
</div>
I've written some code for the event that the link attached to a menu item with a child is clicked. Inside that is an if statement, which says if the list item is already marked as open, then close (slideUp) the submenu and remove the open marker, otherwise open (slideDown) the submenu and add the open marker.
By default, all sub-menus are initially closed and have no open marker. When I click on a parent menu item, the submenu slides open and then immediately slides shut again. I stepped through it in Firebug and the code is literally running through the if statement and at the end of the else statement it is returning to the beginning and doing the if statement a second time before stopping. I've tried adding "return false;" statements but they make no difference.
jQuery("li.menu-item-has-children > a").click(function(event){
event.preventDefault();
if(jQuery(this).parent().hasClass("open")){
jQuery(this).next("ul").slideUp(100);
jQuery(this).parent().removeClass("open");
return false;
} else {
jQuery(this).next("ul").slideDown(100);
jQuery(this).parent().addClass("open");
return false;
}
return false;
});
I don't understand?!? Any help will be gratefully received. Thank you!
Ronel
Edit: So I got it functioning in a jsfiddle (https://jsfiddle.net/ronelz/hvy9wxy9/12/) and the code works perfectly?! However on the actual website it runs the if statement twice i.e. instead of opening a submenu, it opens and then immediately closes the submenu again.
Any suggestions on where I could begin looking for the cause would be gratefully received. Thank you.