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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:55:59+00:00 2026-05-27T19:55:59+00:00

I have a mousedown event attached in an anchor element which does many stuffs.

  • 0

I have a mousedown event attached in an anchor element which does many stuffs.

I also have a mousedown event attached to the document, and because of bubbling this event is called whenever the event attached to anchor is triggered. This is not what I want.

Can I bind a event with delay?
I dont want to use stopPropagation.

$('a').mousedown ->
  ...
  openWindow()
  $(document).mousedown ->
     ...
     closeWindow()

Edit

I create a hack

$.fn.onBubble = (events, selector, data, handler) ->
    setTimeout => 
        this.on events, selector, data, handler 
    , 0

Work but like very ugly

  • 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-27T19:56:00+00:00Added an answer on May 27, 2026 at 7:56 pm

    As one of the comments mentions, the only way to stop events from bubbling is with stopPropagation. That said, if there are both conditions where you do want to prevent bubbling and others where you do not, you can put event.stopPropagation() into an if-statement:

    $(...).mousedown(function(event) {
        if(/* some condition */) { event.stopPropagation(); }
    });
    

    Alternatively you can add a conditional to the event handler attached to the document. For example:

    $(document).mousedown(function(event) {
        if($(event.target).is("a")) {
            return; // if the element that originally trigged this event
                    // (i.e. the target) is an anchor, then immediately return.
        }
        /** code that runs if event not from an anchor **/
    });
    

    This snippet uses $.fn.is to determine if the event was triggered by an anchor. If it is generated by an anchor, the code immediately returns, which in effect ignores the event bubble.

    EDIT in response to comment:

    If I understand correctly, you want to close the window, if the user clicks on anything that is not in the window. In that case try this:

    function whenWindowOpens { // Called when the window is opened
        var windowElement; // Holds actual window element (not jQuery object)
        $(document).bind("mousedown", function(event) {
            if($.contains(windowElement, event.target)) {
                return; // Ignore mouse downs in window element
            }
    
            if($(event.target).is(windowElement)) {
                return; // Ignore mouse downs on window element
            }
    
            /** close window **/
    
            $(this).unbind(event); // detaches event handler from document
        });
    }
    

    This is basically a variation on the second solution suggested above. The first two if-statements ensure the mouse down did not occur in (using $.contains) or on (using $.fn.is again) the windowElement. When both statements are false, we close the window and unbind the current event handler. Note that $.contains only takes raw DOM elements — not jQuery objects. To get the raw DOM element from a jQuery object use $.fn.get.

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

Sidebar

Related Questions

I have 2 sibling-nodes with 'position absolute' which both handle mousedown event. How can
I have the following code snippets $(document).mousedown(function(event) { doSomething(); } I can capture the
I have a link element where I capture the mousedown event and stop the
I have the following code $(document).ready(function(){ $('img').mousedown(function(event) { $('p').text('Mouse Clicked x =' + event.screenX
I have an Adorner which adornes a Border (please see screenshot below). The MouseDown
I have this CustomControl which contains an InkPresenter and an Image . The image
I have an NSStatusItem that has an NSMenuItem which contains a custom NSView. this
If you have an Winform MouseDown event set to a method, how do you
Since the Image control doesn't have a Click event, I simulate it using MouseDown
I have a subclass of NSView where I'm handling the -mouseDown: event to get

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.