Collapse all ULs other than the clicked one and its parent ULs

Andy G

I have a navbar with nested ULs as shown further below. I'm using a simple class, and toggling it, to hide menus/submenus:

nav ul ul {
    display: none;
}
.showIt {
    display: block;
}

On clicking a link with href of "#" I'm trying to hide all the other ULs, except the ones containing the clicked item, using:

$("nav").on('click', "a[href='#']", function () {
    var $thisUl = $(this).next('ul');
    $('nav ul').not($thisUl).each(function () {
        if (!($.contains($(this), $thisUl)))
            $(this).removeClass('showIt');
    });
    $thisUl.toggleClass("showIt");
});

This isn't working for some reason, as it doesn't expand the clicked submenu. In fact, it collapses the menu containing this submenu.

Here's the nav structure for reference:

<nav>
<ul class="navbar">
    <li>
        <a href="index.html">Home</a>
    </li>
    <li>
        <a href="newsletter.html">News/Hints &amp; Tips</a>
    </li>
    <li>
        <a href="#">Courses</a>
        <ul>
            <li>
                <a href="#">IT Courses</a>
                <ul>
                    <li><a href="courses/access-main.html">Access</a></li>
                    <li><a href="courses/excel-main.html">Excel</a></li>
                    <li><a href="courses/onenote-main.html">OneNote</a></li>
                    <li><a href="courses/outlook-main.html">Outlook</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Microsoft Certified</a>
                <ul>
                    <li><a href="ms-certified-courses/mos-main.html">Microsoft Office Specialist(MOS)</a></li>
                    <li><a href="ms-certified-courses/mosexpert-main.html">Microsoft Office Specialist(MOS) Expert</a></li>
                    <li><a href="ms-certified-courses/mosmaster-main.html">Microsoft Office Specialist(MOS) Master</a></li>
                </ul>
            </li>
            <li><a href="http://external_link.co.uk" target="_blank">PD Courses</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Materials</a>
        <ul>
            <li><a href="courseware.html">Course Materials</a></li>
            <li><a href="materials/latest.html">Latest Downloads</a></li>
            <li><a href="materials/access.html">Access</a></li>
            <li><a href="materials/excel.html">Excel</a></li>
        </ul>
    </li>
    <li>
        <a href="testimonials.html">Testimonials</a>
    </li>
    <li><a href="ourteam.html">Our Team</a></li>
    <li><a href="contactus.html">Contact Us</a></li>
</ul>
</nav>

How can I collapse (remove the class of) all ULs other than the current one, and its parent ULs.

user4639281

contains() requires dom elements

Just change

if (!($.contains($(this), $thisUl)))

to

if (!($(this)[0].contains($thisUl[0])))

(Demo)

And here is the vanilla javascript solution for future viewers.

(function () {
    "use strict";
    document.querySelector('nav').onclick = function (e) {
        if (e.target.getAttribute('href') == '#') {
            var thisUl = e.target.nextElementSibling;
            var lists = document.querySelectorAll('nav ul.showIt'),
                list;
            for (var i = 0; list = lists[i]; i++) {
                if (!list.isSameNode(thisUl) && !list.contains(thisUl)) list.className = list.className.replace(' showIt', '');
            }
            thisUl.className += ' showIt';
        }
    };
})();

(Demo)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Center UL in parent and ignore other floating ULs

How to add and toggle all li of child uls to parent ul below specific clicked li

How to collapse all the nested Accordions if clicked on its parent Accordion?

Mulitple ULs handled by one click event in jquery

Find all previous LIs in group of ULs

getElementsByClassName not finding uls

Horizontally Align ULs In Different

How to make all items invisible other than the one I clicked in RecyclerView?

Bootstrap collapse expanding all cards when one is clicked

sorting nested list for uls with 0 elements

CSS to target last UL when there is two ULs

what permissions are needed for writing to uls logs?

selecting only those li having ULs

Avoid multiple uls to create a "diagonal" list

parent *ngfor has a child ngfor with a gallery - clicking the image affects all other child ngfor's - how to only affect the one being clicked

Collapse all other accordion items when one is expanded

How to set collapse on all other divs when one div is open?

XML attribute with namespace other than its parent's is deserialized as null

jQuery - toggle element that is clicked and hide all other (not immediate parent)

HTML buttons, toggle when button clicked and toggle all other buttons (collapse info)

mystery of the height of uls in positioned divs: How does it even work?

How to wrap multiple groups of LIs within a string with ULs in php

Split UL in to two columns, but don't break nested ULs

On Click of Anchor, How to target a LI that does not have any ULs

Hide all other rows with same class except the one clicked

Hide the all other div's except one which is clicked

Collapse any runs of lines longer than n into one, maintaining integrity of any other instances of the line

Create a class that contains all functions of its parent class except one

How to close all other open sub-menu when other parent menu item is clicked?