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 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

I need to apply a style in code ike this: TextBlock.Style = TryFindResource(MyStyle) as
Need a way to allow sorting except for last item with in a list.
Need to an expression that returns only things with an I followed by either
I need to apply some xml templates to various streams of xml data (and
My current program need to use programatically create a XPathExpression instance to apply to
Need a function that takes a character as a parameter and returns true if
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
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
Good afternoon people's I have built a jquery plugin that I use on my

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.