On my responsive dropdown menu I want to be able to do the following when a menu item is clicked:
- If link has no children then functions as normal
- If parent link has children then default is prevented for parent and children are displayed
- The parent and children links now function as normal (i.e. no preventDefault after click to show child items)
The (non-functining) JQuery I've done is below and here is my Fiddle (Edited - simplified the example)
var parentWithChildren = $('region-responsive-nav ul');
//var parentWithChildren = $('region-responsive-nav ul').children('has:ul').length > 0 );
$('.region-responsive-nav ul ul').hide();
//prevent default once
if (parentWithChildren.children('has:ul').length > 0 ) {
$('.region-responsive-nav ul li').one(function(event) {
$(this).children('ul').slideToggle();
event.preventDefault();
});
}else{
//open link immediately
}
See below for the markup. (Since simplified in my edit. Please note that it has two <ul>
inside the same <li>
and there is not much I can do about this.
<nav class="region-responsive-nav">
<ul>
<li>One</li>
<li>Two
<ul class="sub">
<li>Two A</li>
<li>Two B</li>
</ul>
</li>
</ul>
<ul>
<li>Three</li>
<li>Four
<ul>
<li>Four A</li>
<li>Four B</li>
</ul>
</li>
</ul>
</nav>
I think I'm fairly close (at least in concept) so ainy pointers would be appreciated.
Thanks in advance