Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:40:50+00:00 2026-06-01T16:40:50+00:00

I was making a menu today, and I stumbled on this curious case, with

  • 0

I was making a menu today, and I stumbled on this curious case, with the following HTML:

<div id="list1">
    <ul>
        <li>1</li>
        <li>2 - Here is a Submenu
            <ul>
                <li>3</li>
                <li>4</li>
            </ul>
        </li>
    </ul>
</div>

Then i used the following js:

$('#list1 li').click(function(){
   $('.list-item').removeClass('list-item');
   $(this).addClass('list-item');
});

When I do this the class would only be applied to the outer <li>, but I wanted it to be applied to both the parent and child LIs.

Now my question is, how can I handle this “returns” so I apply the classes to both the parent and child LIs?

How does js handle these type of selectors? Is the event really running twice and removing the last class that was set?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:40:52+00:00Added an answer on June 1, 2026 at 4:40 pm

    The issue is that $('#list1 li') can select both levels of li tags inside #list and apply a click handler to both. In addition, you are allowing the click to propagate up so it may be seen by more than just the one you click on.

    If you only want the outer level, then you should use direct child specifications like this. and that will isolate what click handler is actually installed:

    $("#list > ul > li") 
    

    This will only get the top level li tags. And, the whole code would look like this:

    $('#list1 > ul > li').click(function(){
       $('.list-item').removeClass('list-item');
       $(this).addClass('list-item');
    });
    

    If you want to isolate the removeClass operation to just the other LI tags at the same level, not across the entire document, then you can use something like this:

    $('#list1 > ul > li').click(function(){
       $(this).addClass('list-item').siblings().removeClass('list-item');
    });
    

    Your comments have me a bit confused about this LI tags you want clickable. If you actually want the lower level to be clickable and then apply the list-item class to both the one that was clicked and the parent LI, then you can do that like this:

    $('#list1 > ul > li > ul > li').click(function() {
       $("#list1 .list-item").removeClass('list-item');
       $(this).parents("li").add(this).addClass('list-item');
       return(false);   // stop event propagation
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a menu in jquery. You can see it at http://mywash.dk/testtest/index.html The submenu
I'm making a menu in jquery. You can see it at http://mywash.dk/testtest/index.html The submenu
I am making a menu for an app and was pointed to this useful
I'm pretty new to HTML and CSS. I'm making a menu bar horizontal and
I'm making a very basic menu for my site and I have used the
I am making a navigational menu in html and css, but i want the
I am making a top menu, and it looks like this: https://i.stack.imgur.com/5O5G5.png The contact
I am making a menu test for the employees at my restaurant. The plan
I'm making a game and in the menu I want to display the text
I'm making a CSS dropdown menu. So, when the mouse hovers one menu item,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.