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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
niceoneishereAuthor Commented:
Anyway to close the sub-menu when its looses the focus or moved away.

Thanks
1
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
Juana VillaFront-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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.