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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:20:10+00:00 2026-05-13T06:20:10+00:00

I find myself very often in the situation that I open an element in

  • 0

I find myself very often in the situation that I open an element in a web page – e.g. a drop-down menu – that I want to close if the user clicks anywhere on the page except the element itself.

To keep things simple, I have mostly written the code myself instead of employing some drop-down menu class.

However, I have never managed to build an implementation of this that was completely satisfying: Event handling and bubbling would work differently in different browsers, there would be the need for nasty workarounds, in some situations clicking the drop-down button would start closing it in the same moment, and so on.

Is there a Prototype based, authoritative, best practice to do this? Something that works across browsers – IE6 being a plus but not a requirement?

Just this:

  • click on a button – an element opens
    (e.g. an absolutely positioned drop-down menu).
  • click within the element – the element stays open.
  • click on the button that opened the element – the element stays open.
  • click anywhere else on the page – the element closes.

I need help with the event handling part only, the displaying of the menu is totally secondary.

  • 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-13T06:20:10+00:00Added an answer on May 13, 2026 at 6:20 am
    Event.observe(document, 'click', function (event) {
      switch (event.element().id) {
        case 'example_id':
          // do different stuff depending on element clicked
          // ofc u don't need to pass id, u can simply throw an element itself
          break;
        default:
          // do close action
          break;
      }
      // also check Event.findElement();
    });
    

    You can also add specific classes to the items you don’t want to trigger close action and check it inside

    if (!event.element().hasClassName('dont_close'))   
      Element.remove(selectDOMElement);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping that someone can help clear up this very annoying situation I find myself
I find myself very often creating an object that has no public methods and
I often find myself in the situation where I only want to present and
Very often, I find myself using a callback function and I don't have its
I find myself often with a situation where I need to perform an operation
I find myself trying to convert constructor parameters to their right types very often
This is a very simple question. I often find myself wanting to create a
I often find myself in need of writing down a future Visual Studio solution
I am not a very orderly person at times and I often find myself
I often find myself in need of very complex SQL examples when testing abstraction

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.