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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:10:06+00:00 2026-05-22T12:10:06+00:00

In my navigation the user clicks a link to open a submenu. as it

  • 0

In my navigation the user clicks a link to open a submenu. as it stands the submenu stays open until it’s parent link is clicked again. I’d like to have it so if the user clicks anywhere on the page but inside the submenu that the submenu hides.

The code i’m trying to get to work is:

for(var i=0; i<$('#topNav ul li').length; i++){
    if(openMenu[i] == 1){ 
        //i use an array to keep track of which submenu is open
        //all 'closed' or hidden submenus are assigned a value of 0 in the array,
        //the open submenu gets a value of 1
        $('body').not('#submenu'+i).click(function(){
            $('#submenu'+i).hide();
        });
    }   
}

the problem is nothing happens when i click on the body. I’ve set up a basic example on jsfiddle, it’s here.

is there something i’m missing? can you not use the body tag as a selector for click events?

  • 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-22T12:10:08+00:00Added an answer on May 22, 2026 at 12:10 pm

    You might want to try something like this:

    JS:

    $(function(){
        var visible = false;
        $('button').click(function(e){
            $('.menu').hide();
            var $ref = $(this).attr('ref');
            $('#menu'+$ref).show();
        });
    });
    

    HTML:

    <div class='menu' id='menu1' style='background-color:#f00;'></div>
    <div class='menu' id='menu2' style='background-color:#ff0;'></div>
    <div class='menu' id='menu3' style='background-color:#f0f;'></div>
    <button ref='1'>Click Me 1</button>
    <button ref='2'>Click Me 2</button>
    <button ref='3'>Click Me 3</button>
    

    Fiddle: http://jsfiddle.net/maniator/46wuy/4/

    This reduces the need to have a for loop on every click

    EDIT
    To address your comment below:

    $('*').click(function(e){
         e.stopPropagation();
        if($(e.currentTarget).not($('.menu, button')).length  > 0){
             $('.menu').hide();
        }
    });
    

    Fiddle: http://jsfiddle.net/maniator/46wuy/7/

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

Sidebar

Related Questions

I have some java script that work behind a navigation menu, the user clicks
When a user clicks a link, I would like to send an AJAX request
I have a WebBrowser control which shows some html. When user clicks some link
I have a flashlite3 application with navigation consisting of icons the user can browse
In .NET (C#, specifically), how can I stop navigation? Situation: User has clicked a
I have created a navigation menu where, when clicking on a link, a jquery
I have some jQuery code like this: // Called right away after someone clicks
I have a function that advances a slideshow image when a user clicks a
I have an AJAX navigation and I would like to scroll the page to
I have index page with navigation menu like this man caps shirt jeans woman

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.