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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:02:17+00:00 2026-06-01T14:02:17+00:00

I am using the code below to show/hide a navigation menu on click of

  • 0

I am using the code below to show/hide a navigation menu on click of an anchor.

The problem that I am having is that the first time the page loads, I have to click the anchor twice to get the menu to show. After that, I can toggle the menu with a single click. I can also click off the menu anywhere on the document to hide it.

Does anyone see a problem with the code below or know of a better way for me to hide the menu when a user clicks off the menu?

$('#aToggleQuickNavigation').click(function () {
    $('#ulQuickNavigation').toggle();
});

$('html').click(function () {
    if ($('#ulQuickNavigation').css('display') == 'block') {
        $('#ulQuickNavigation').css('display', 'none');
    }
    $('#aToggleQuickNavigation').click(function (event) {
        event.stopPropagation();
    });
    $('#ulQuickNavigation li a').click(function (event) {
        event.stopPropagation();
    });
});
  • 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-01T14:02:18+00:00Added an answer on June 1, 2026 at 2:02 pm

    Hide it if the click event bubbles all the way up to the body:

    $("body").click(function(){
      $("#ulQuickNavigation").hide();
    });
    

    If the user clicks anywhere on the menu, you are already preventing the propagation of that click, so the body will never hear of it, and as such it will not be hidden.

    Another thing I noticed is that you’re binding your click events within the click event handler of the HTML element. This can be pretty problematic. Each time a click bubbles up to the HTML element, you will be binding more and more click events.

    Just stick with something like this for now:

    // Any click that arrives at the body should close my navigation
    $("body").click(function(){
      $("#ulQuickNavigation").hide();
    });
    
    // Prevent clicks on nav from reaching the body
    $("#ulQuickNavigation").click(function(e){
      e.stopPropagation();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to show hide my subview: [self.view addSubview:selectorView]; [selectorView setFrame:CGRectOffset([selectorView
I am using the code below to show only the first 3 items in
I am using the code below to show and hide a div called map
I'm having a problem with the jQuery hide() and show() methods. Below is a
In past, I am using Listview and using below code can show a particular
Using the code below, I want to keep the same menu div opened in
I have a loading image that works using the code below. It shows the
I am using jquery addClass code to show/hide elements when rolling over a div.
Using the code below, I am attempting to fill a Canvas with UIElements and
Using the code below (from a console app I've cobbled together), I add seven

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.