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 405309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:23:18+00:00 2026-05-12T17:23:18+00:00

I need to apply a jQuery.click to the first level items only. How do

  • 0

I need to apply a jQuery.click to the first level items only. How do I do that?

Here is my list:

<ul id="adminMenu">
  <li id="A">
    <h3><a href="">Item 1</a></h3>
  </li>
  <li id="B">
    <h3>Item 2</h3>
    <ul style="display: block;">
      <li id="a1"> Sub Item 1 </li>
      <li id="a2"> Sub Item 2 </li>
      <li id="a3"> Sub Item 3 </li>
    </ul>
  </li> 
  <li id="C">
    <h3>Item 3</h3>
    <ul style="display: none;">
      <li> Sub Item 4 </li>
      <li> Sub Item 5 </li>
    </ul>
  </li> 
</ul>

And here is the jQuery

jQuery('#adminMenu > li').click(function(){
  alert('test');
});

UPDATE
The Alert should not fire when I click a Sub Menu item, only when I click list item A, B or C.

SOLUTION 1
This is working code based on Marcels suggestion.

  jQuery('#adminMenu > li > h3').click(function(e) {
    var activeUL = jQuery("#adminMenu > li ul:visible");
    var activeLI = jQuery("#adminMenu > li ul:visible").parent('li:first');
    var clicked = jQuery(this).parent('li:first');
    // Close submenu
    activeUL.hide('fast');
    // Open submenu
    if( activeLI.attr('id') != clicked.attr('id') )        
      clicked.children('ul').show('fast');
  });

SOLUTION 2
This is working code based on Eyelids suggestion.

  jQuery('#adminMenu > li').click(function(e) {
      var clicked = jQuery(e.target);
      // Ensure we're checking which list item is clicked,
      // other children should be allowed
      if(!clicked.is('li') && clicked.parents('li').length > 0) {
          // :first ensures we do not select higher level list items
          clicked = clicked.parents('li:first');
      }
      // If clicked list item is a child of another list item, we'll exit here
      if(!clicked.is('#adminMenu > li')) {
          return;
      }
    var activeUL = jQuery("#adminMenu > li ul:visible");
    var activeLI = jQuery("#adminMenu > li ul:visible").parent('li:first');

    // Close submenu
    activeUL.hide('fast');
    // Open submenu
    if( activeLI.attr('id') != clicked.attr('id') )        
      clicked.children('ul').show('fast');
  });

Thanks guys! I would never have managed this without your help! 🙂

  • 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-05-12T17:23:18+00:00Added an answer on May 12, 2026 at 5:23 pm
    jQuery('#adminMenu > li').click(function(e) {
        var clicked = jQuery(e.target);
        // Ensure we're checking which list item is clicked,
        // other children should be allowed
        if(!clicked.is('li') && clicked.parents('li').length > 0) {
            // :first ensures we do not select higher level list items
            clicked = clicked.parents('li:first');
        }
        // If clicked list item is a child of another list item, we'll exit here
        if(!clicked.is('#adminMenu > li')) {
            return;
        }
        alert('test');
    });
    

    Updated to exit if clicked list item is not an immediate descendant of #adminMenu.

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

Sidebar

Related Questions

Is there any good JavaScript / jQuery plug-in that will 'quicklook' an image on
I'm writing a jQuery plugin that has a couple of callbacks, and am at
Given the following jquery code.. jQuery.fn.sliding = function () { return this.each(function () {
I am trying to figure out what is wrong with this code, I need
I'm incredibly new to jQuery and am wondering if my code will work when
I'm trying to impliment a confirmation box, but I need to edit the text
I have the following HTML/JS that shows an initial value, removes it when you
Preface: I am sure this is incredibly simple, but I have searched this site
I'm injecting Javascript into a page w/ Greasemonkey, doing some processing on the body

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.