jQuery mega menu display only the clicked parent submenu.

I have a simple Mega Menu. I am trying to display the sub menu of the parent menu item clicked. But for some reason when I click the parent item all the sub menu's are displaying instead of the clicked parent item sub menu. What I am doing wrong.

Here is my JSFiddle link

https://jsfiddle.net/jokcLjkb/4/

here is my js code i am using

$(document).ready(function() {
    $("a.drop").on('click', function(e) {	
        e.preventDefault();
        $("ul.mainNav li > .drop-full-col").css("display","block");
    });
});

Open in new window


Thanks and Appreciate it
LVL 2
niceoneishereAsked:
Who is Participating?
 
Juana VillaConnect With a Mentor Front-end DeveloperCommented:
Hello,

Link updated

With js I am adding a class on click and removing the class when the user clicks outside the nav or when hovers outside the nav

/* On click we add a class and remove it to any other element who might have that class*/
$("a.drop").click( function(e) {	
  $(this).parents('.full-col').toggleClass('openMenu').siblings().removeClass('openMenu');
});

/*When click outside the nav we close the nav*/
 $(document).mouseup(function(e) 
{
    var container = $(".full-col");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
        container.removeClass('openMenu');
    }
});

/*When hover outside the nav we close the nav*/
$( ".drop-full-col" ).mouseleave(function() {
  $( ".full-col" ).removeClass('openMenu');
});

Open in new window


and with css we show the nav when the div has the class we are adding/removing with js

 ul.mainNav li.openMenu .drop-full-col {
  display: block;
} 

Open in new window


I hope this helps :D
0
 
Juana VillaFront-end DeveloperCommented:
Hi,

The problem was that every time a "a.drop" was clicked ALL of the subnavs were display block. We need to only open the one clicked. I updated the link you provided

1. With js I am toggling a class to the parent li.full-col when the button is clicked and removing that class from the other ones that were not clicked. This way, only one it will be open at a time.

$("a.drop").click( function(e) {	
  $(this).parents('.full-col').toggleClass('openMenu').siblings().removeClass('openMenu');
});

Open in new window


2. With css I am showing the sub nav

ul.mainNav li.openMenu .drop-full-col {
  display: block;
}

Open in new window


I hope this helps. Let me know if you have questions.
1
 
niceoneishereAuthor Commented:
Brilliant and thanks for reply. Anyway to close the sub-menu when its clicked anywhere  else like non menu area.

Thanks again
1
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
niceoneishereAuthor Commented:
Anyway to close the sub-menu when its looses the focus or moved away.

Thanks
1
 
Juana VillaFront-end DeveloperCommented:
Hi, I updated again the link you provided.

I think the best solution is to use only css. This way when the users hovers on the top menu nav: we will show the right submenu and when the section looses focus, the mouse is moved away, or the user clicks somewhere else (outside the dropdowns) the submenu will be closed.

I removed the js and only use this css.

Note: I used also "focus" because on mobile devices there is no hover.

ul.mainNav li.full-col:hover .drop-full-col,
ul.mainNav li.full-col:focus .drop-full-col{
  display: block;
}

Open in new window


I hope this helps.
0
 
niceoneishereAuthor Commented:
Thanks again for replying but our customers are getting frustrated with the hover as the sub menu opens as soon as they hover and they are complaining about it as this big sub menu is coming in the way.

Sorry to be a pain but I have to go with the click option just need to know to show the sub menu only a parent item is clicked

Thanks again
1
 
niceoneishereAuthor Commented:
Simply Brilliant. I have extended it to support touch on tablets and ipads. Here is js code

$(document).ready(function() {

$("a.drop").click( function(e) {	
 e.preventDefault();
  $(this).parents('.full-col').toggleClass('openMenu').siblings().removeClass('openMenu');
});

 $(document).bind('mouseup touchend', function(e){
    var container = $(".full-col");
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
        container.removeClass('openMenu');
    }

});

$( ".drop-full-col" ).on('mouseleave touchend',function() {
  $( ".full-col" ).removeClass('openMenu');
});

});

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.