code opens all submenus on clicking primary menu li item

user4774980

The PHP code generates a navigation menu of this structure -

<ul class="menu">
    <li><a href="link">LINK 1</a></li>
    <li><a href="link">LINK 2</a>     <!-- click disabled -->  
        <ul class="sub-menu">
            <li><a href="link">LINK 3</a></li>
            <li><a href="link">LINK 4</a></li>
        </ul>
    </li>
    <li><a href="link">LINK 5</a></li>
    <li><a href="link">LINK 6</a>     <!-- click disabled -->
        <ul class="sub-menu">
            <li><a href="link">LINK 7</a></li>
            <li><a href="link">LINK 8</a></li>
            <li><a href="link">LINK 9</a></li>
        </ul>
    </li>
    <li><a href="link">LINK 10</a></li>
    <li><a href="link">LINK 11</a></li>
</ul>

Instead of having to open sub-menu using hover, I want to make it open using click
The jQuery for click is:

$(document).ready(function(){
    var li = $('.menu>li',this).has('.sub-menu');
    $('>a',li).click(function(e){
        e.preventDefault();
        $('.sub-menu',li).toggle();
    });
});

The only problem is that while clicking, all the ul with class .sub-menu gets opened. I only want the sub-menu of the currently clicked anchor tag to open. How do I do that?

brroshan

Try this:

Working example https://jsfiddle.net/cjvwkvq9/

$("li > a")
    .on("click", function (e) {
    $(e.target).next().toggle();
});

Where $(e.target) is the clicked element.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove all padding between my link <a> and the list item it lives in <li>? or... click on <li> opens the link inside of it?

Angular - Hide sidebar menu after clicking a menu item

False Activity opens by clicking on an Item on the Navigation drawer

No reaction after clicking on li in custom context menu

Prevent MDC Menu from closing after clicking first menu item

Recyclerview - Clicking on results displayed by search opens incorrect item

Cut, Copy, Paste, Select All Functions - How to simply allow default handling after clicking on menu item?

why when click on an item, all item opens

Changing div text value by clicking on a li item

Close submenus of one menu item when another menu item's button ('cousin' of first) is clicked with Javascript and no jQuery

Getting Primary ID on clicking a List Item in Android

Submit form by clicking on drop down menu item

force close when clicking menu item android

WPF how to force menu item bindings to update when menu opens

AngularJS "Accordion" style menu + dynamic data + submenus + using li ---- working example

Clicking autocomplete menu item using Selenium

submenus of current menu item shows the current menu color

On clicking a menu item on Toolbar shows a List view

Drop down menu opens all sub menus

Add class to all page elements with certain class after clicking at menu item

Change img src by clicking li item CSS

Clicking on <li> list item not using Selenium Java

Reverting hamburger animation after clicking the menu item

Clicking on navigation drawer listview item opens unclicked activity every time

Activity not opening when clicking menu item

all my li menu item close the menu not do what they should

Close responsive menu on clicking a <li>

MegaMenu JS issue - All menu links triggering first <li> item only

All my sidebar menu opens at once in ReactJS