in Education by
I have a menu which opens up a submenu on click. If a link has a dropdown menu (has class header--has-submenu), then on click, I'm making another element (.header__subContainer) appear. The issue I'm having is with the logic. For example, in my demo, I have two links, what we deliver and about us, both have submenus. I want a user to be able to click onto a link to open it and then click on the same link to close it (standard UX). However, I also want the user to be able to click on one link (let's say 'what we deliver) and then click onto about usto show that menus dropdown (then the user can click theabout us` link again to close the menu). I've tried toggleClass but this causes issues with the last scenario above. Here's a demo: $(".header--has-submenu a.header__parentLink").click(function(e) { e.preventDefault(); console.log("click"); var id = $(this).attr('data-menu'); // add active state styles to link to showcase which menu is open $("li.header--has-submenu").removeClass("header--has-submenu--active"); $(this).parent().addClass("header--has-submenu--active"); // open subContainer (black div that contains all submenus) if ( $(".header__subContainer-menu").hasClass("header__subContainer-menu--active") ){ $(".header__subContainer").removeClass("header__subContainer--active"); } else { $(".header__subContainer").addClass("header__subContainer--active"); } // remove active class from submenu, so only one menu is open at one time $(".header__subContainer-menu").removeClass("header__subContainer-menu--active"); // open the relevant menu $(this).closest(".header__subContainer").addClass("header__subContainer--active"); $(".header__subContainer-menu[data-menu='"+id+"']").toggleClass("header__subContainer-menu--active"); }); .header { padding: 30px 0; } .header__parentUl * { color: #FFFFFF; } .header__li { margin: 0 20px; } .header__subContainer { display: none; } .header__subContainer--active { display: block; } .header__subContainer-menu { display: none; } .header__subContainer-menu--active { display: block; } .header--has-submenu--active a { color: green; } .background--black { background: #000000; } .reset-list { list-style-type: none; margin: 0; padding: 0; } .color--white { color: #FFFFFF; }
Run code snippetExpand snippet JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Something like this should work: $(".header--has-submenu a.header__parentLink").click(function(e) { e.preventDefault(); let id = $(this).attr('data-menu'); // Check if link already active let wasOpen = $(this).parent().hasClass("header--has-submenu--active"); // remove all active classes $(".header--has-submenu").removeClass("header--has-submenu--active"); $(".header__subContainer-menu").removeClass("header__subContainer-menu--active"); $(".header__subContainer").removeClass("header__subContainer--active"); if(!wasOpen){ // the clicked link was not open // add active classes to the 3 elements $(this).parent().addClass("header--has-submenu--active"); $('.header__subContainer').addClass('header__subContainer--active'); $(".header__subContainer-menu[data-menu='"+id+"']").addClass("header__subContainer-menu--active"); } });

Related questions

0 votes
    I have a menu which opens up a submenu on click. If a link has a dropdown menu (has class header ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have a Bootstrap modal dialog which contains a form. The modal dialog contains a submit and a cancel ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    10.For Animation, on which menu you will be click to get the effect in MS POWERPOINT. Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    To open a new file usese has to click on which menu and option? class 7 computer Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    *Updated with more relevant code. *Updated again: removing chart groupings results in this error: "Unable to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    2 Fill in the blanks:- 1) You can save, open, edit and close the MS Paint program with the _______ button ... O ans of subject computer Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    To open the table, on the icon that has the required table name in Tables Pane of Database Window (A) Drag ... click (D) Triple Click Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    I have this following datetime64[ns] type timestamps. x1=pd.Timestamp('2018-04-25 00:00:00') x2=pd.Timestamp('2020- ... -04-25 00:53:00 Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    Is it advisable to replace the policy with another policy?...
asked Dec 31, 2020 in Health by Editorial Staff
0 votes
    I have an excel workbook that has more than 20 pages, each has a plenty amount of data. Every ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
0 votes
    Each operation in a demand-driven pipeline can be implemented as an ____ that provides the following ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    I tested out the script below in jsfiddle and it works fine, can someone guide me how to fix it? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    with the help of _____ option we can save another copy of the picture with the another file name ?a. save as b. open c. new d. exit Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    I am developing a Windows 8 metro application that uses semantic zoom in the following way: The zoomed in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    The age old question. Where should you put your business logic, in the database as stored procedures ( or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
...