I'm in the process of creating a responsive drop-down menu. The menu consists of three levels. Each item in this menu is a link. I would like the item from the menu to drop-down with the next menu level, when you click on the same link for the first time, I would like the user to be redirected to the page he chose. I managed to create a script that adds a class, so that the menu will drop down and close with a second click. Now I would like to add functionality that will allow me to redirect.
The active
class switches the class with property: display: block
;
I've already tried to add the preventDefault()
function to my script, which blocks redirection from links, but I'd like to unlock it with a second click.
JQuery code:
$(document).ready(function(){
$('.menu-item-has-children').click(function(e){
e.preventDefault();
$(this).children('.sub-menu').toggleClass("active");
return false;
});
});
HTML code:
<ul id="main-menu" class="main-nav">
<li id="menu-item-759" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-759"><a href="http://example.com">Page 1</a></li>
<li id="menu-item-760" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-760"><a href="#">Page 2</a></li>
<li id="menu-item-761" class="menu-item menu-item-type-post_type menu-item-object-page current-page-ancestor current-menu-ancestor current_page_ancestor menu-item-has-children menu-item-761"><a href="#">Page 3</a>
<ul class="sub-menu">
<li id="menu-item-762" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-762"><a href="#">Subpage 1</a></li>
<li id="menu-item-763" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-763"><a href="#">Subpage 2</a></li>
<li id="menu-item-764" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-764"><a href="http://example.com">Subpage 3</a></li>
<li id="menu-item-765" class="menu-item menu-item-type-post_type menu-item-object-page current-page-ancestor current-menu-ancestor current-menu-parent current-page-parent current_page_parent current_page_ancestor menu-item-has-children menu-item-765"><a href="#">Subpage 4</a>
<ul class="sub-menu">
<li id="menu-item-766" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-192 current_page_item menu-item-766"><a href="http://example.com" aria-current="page">SubSubPage 1</a></li>
<li id="menu-item-767" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-767"><a href="http://example.com">SubSubPage 2</a></li>
</ul>
</li>
</ul>
</li>
</ul>