Home > Enterprise >  JS Toggle Submenu within Mega Menu
JS Toggle Submenu within Mega Menu

Time:05-09

The issue I'm having is, creating a submenu inside another menu.

Demo: LIVE DEMO (Important, cause CSS is needed as well

$(function () {
  // Desktop Menu
  var categoriesMenu = $(".list-ausbildung-categories li");
  var triggerMenu = $(".dropdown-submenuSide");
  var highlightsList = $(".list-ausbildung-highlights");
  var submenuList = $(".list-ausbildung-submenu");

  $('.list-ausbildung-categories').on('click', 'li', function () {

      if( $(this).hasClass('active') ){
          triggerMenu.removeClass('asg-gray-bg-200');
          $(".dropdown-submenuSide .list-ausbildung-submenu ul").html('');
      } else {
          highlightsList.hide();
          submenuList.show();
          triggerMenu.addClass('asg-gray-bg-200');

          $('li.active').removeClass('active');
          $(this).addClass('active');

          var subMenu = $(this).find(".dropdown-submenu").html();

          $(".dropdown-submenuSide .list-ausbildung-submenu ul").html(subMenu);
      }
  });

  $('.asg-megamenu div[class^="col"]:first-child').on('click', function () {
      categoriesMenu.removeClass('active');
      triggerMenu.removeClass('asg-gray-bg-200');
      submenuList.hide();
      highlightsList.show();
  });
});

I'm having this Bootstrap Mega Menu, which also contains a submenu (Column 2). On click, it should hide Column 3, and show the submenu items. (it does its job)

Currently, I'm grabbing the submenu content with jquery html() and then placing it on the third column (probably not the cleanest method).

The problem: whenever I close a submenu and click again, it won't open back.

CodePudding user response:

It looks like currently, the active class isn't removed on the second click. Instead, it just clears out the HTML of column three. We could fix that by adding a line to remove the active class when we hide the submenu.

if( $(this).hasClass('active') ){
    $(this).removeClass('active'); // add in this line here so it will trigger properly on the next click
    triggerMenu.removeClass('asg-gray-bg-200');
    $(".dropdown-submenuSide .list-ausbildung-submenu ul").html('');
}
  • Related