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

  • SEARCH
  • Home
  • 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 8405859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:56:29+00:00 2026-06-09T22:56:29+00:00

ok, I have been through some basics but I’m not able to get this

  • 0

ok, I have been through some basics but I’m not able to get this done. Problem is I don’t want to specify id or class for any ul or li and if I click on a header included in the ul I want the li to be toggled (show/hide). How can I select the li under a header without specifying id and class???. Here is what I’ve tried :

<nav>
    <ul>
        <header>Menu 1</header>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
    </ul>
    <ul>
        <header>Menu 2</header>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
    </ul>
    <ul>
        <header>Menu 3</header>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
    </ul>
</nav>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
    $(function(){
        $('ul li').hide();
        $('ul header').css('cursor', 'pointer');
        $('ul header').click(function(e) {
            $(this).next().toggle();
        });
    });
</script>

Ok. This one just toggles the last child of ul but no all of them. Any help would be appreciated, Thanx.

  • 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-09T22:56:30+00:00Added an answer on June 9, 2026 at 10:56 pm

    If you use $.fn.toggle, it will alternate between two functions when you click. Then, you want to use the $.fn.siblings function to target all of the li elements with the same parent. Here is a demo on jsfiddle.

    $(function(){
        $('ul li').hide();
        $('ul header').css('cursor', 'pointer');
        $('ul header').toggle(function(){
            $(this).siblings('li').show();
        },
        function(){
            $(this).siblings('li').hide();
        });
    });
    

    EDIT: my recommendations were confusing and conflicting when separate, so I will group them all together and explain

    1.) As Felix commented above, it is invalid HTML to have anything but li as a direct decendant of ul. So, you will need to change your markup to something like this:

    <ul>
        <li class="header">Menu 1</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
    </ul>
    

    2.) I would set the following rules in your CSS, so that you don’t have to do them with jQuery:

    nav ul li { display: none; }
    nav ul .header { display: block; cursor: pointer; }
    

    So, with these two recommendations in place, your jQuery would now look like this:

    $(function(){
        $('ul .header').toggle(function(){
            $(this).siblings('li').show();
        },
        function(){
            $(this).siblings('li').hide();
        });
    });
    

    Putting all of my recommendations together in a new fiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been through numerous similar questions on this forum but my issue still
I have been looking through some code on an open source project recently and
I am complete newbie in mod_rewrite , and I have been going through some
i have been trying to pass some variables to model through the controller function
I have now been stuck at this for some time so I though to
I have been through every option to try to find a way to get
I have been browsing through Google for various explanations but I STILL couldn't figure
I have been trying to get up to speed on Named Pipes this week.
I have been searching for hours through all the posts regarding LinkedHashMap but I
I have googled this, searched this, looked through SO and other sites (I've been

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.