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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:31:52+00:00 2026-05-20T01:31:52+00:00

I am currently working on a task to make a pop-up menu, and here

  • 0

I am currently working on a task to make a pop-up menu, and here is my code:

    <script type="text/javascript">
function dropMenu(){
    document.getElementById('dropdown').style.visibility = 'visible';
    document.getElementById('dropdown').style.display = 'block';
        
}
</script>

and here is the div I’m using the js function on:

 <div class="user-menu">
        <ul>
            <li><a href="#"><span>[History]</span></a></li>
            <li>
                <a href="#" id="username" onclick="dropMenu()">
                    <span>${currentUserDetails.firstName} ${currentUserDetails.lastName}</span>
</a>
        <div>
        <ul>
                    <li>
<a href="#" id="dropdown" style="display:none;"> 
    <span>Log Out</span> 
                    </a>
                    </li>
                </ul>
            </div>
          </li>
       </ul>
   </div>

As you can see I’m using the js function to make this menu in the div visible on click. Now here is my question. I need this menu to return invisible after clicking anywhere else. In other words, after clicking out this div, I need the div to be invisible.
Any suggestions?

  • 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-20T01:31:52+00:00Added an answer on May 20, 2026 at 1:31 am

    You can bind a call to a new function that turns the element invisible.

    At the end of the page, add the following script:

    <script language="javascript" type="text/javascript">
        function dropMenu() {
            document.getElementById('dropdown').style.display = 'block';
        }        
    
        function hideMenu() {
            document.getElementById('dropdown').style.display = 'none';
        }        
    
        document.attachEvent('onclick', hideMenu);
    </script>
    

    By attaching the hideMenu function on the documents’s onclick event, every child element of the document (that is, every element inside BODY tag, an the elements insede these elements, and so on), when clicked, will fire the hideMenu function. Well, sort of, as you’ll see ahead.

    It includes the username anchor. So, oddly, as you click the username anchor, it will call the dropMenu function, defined as the onclick event handler for the anchor, an the the event will “bubble up”, causing it to fire at every parent element of the anchor, finally reaching the document, which we have already defined the hideMenu event as the onclick event handler, causing it to hide the dropdown anchor again.

    In fact, this “bubbling” mechanism is what causes the impression of the hideMenu being called for every element on the document, when, in fact, the click event of the clicked element will just bubble up until reach the document, firing his handler.

    So, we have to modify the dropMenu function a bit to avoid this bubbling effect:

        function dropMenu() {
            window.event.cancelBubble = true;
            document.getElementById('dropdown').style.display = 'block';
        }
    

    Now, when we click the username anchor, the dropdown anchor will be displayed, and will stay displayed. And when we click anywhere the page, it will be hidden.

    It happend because we’ve setted the cancelBubble property of the window’s event object to true, indicating that this event may not bubble up, so the document onclick handler shall not fire.

    I hope thie explanation is clear and be useful.

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

Sidebar

Related Questions

I am currently working on a project and my goal is to locate text
I'm currently working on implementing a list-type structure at work, and I need it
I'm currently working on a small script that needs to use gtk.StatusIcon() . For
Currently this is what I need to type to execute my task execute 'mytask'
I am currently working on a task to expose an API as a Web
I am currently a developer working on a very task specific application; the user(s)
I currently working on an issue tracker for my company to help them keep
I'm currently working on creating a new C# project that needs to interact with
I am currently working on a project with specific requirements. A brief overview of
The system I am currently working on requires some role-based security, which is well

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.