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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:53:42+00:00 2026-05-23T21:53:42+00:00

I want to trigger an event when my mouse enters a div, and a

  • 0

I want to trigger an event when my mouse enters a div, and a separate event when it leaves the div using jQuery. I know there are plenty of plugins, code samples, etc. that solve this particular problem. I’m just curious which way would be most efficient in terms of both performance and memory management.

So for instance, assume we have HTML code like this:

<div class="mouse-trap"> 1   </div>
<div class="mouse-trap"> 2   </div>
...
<div class="mouse-trap"> n-1 </div>
<div class="mouse-trap"> n   </div>

1. Separate events

We can maintain the events separately. This code is readable, and easy to work with.

$(".mouse-trap").bind('mouseenter', function() {
    // mouse came in
});

$(".mouse-trap").bind('mouseleave', function() {
    // mouse left
});

2. Nested events

Instead of always keeping a “mouseleave” event around, only create it when it’s necessary and then trash it when we’re done, since there will only ever be one mouseleave event for each mouseenter event.

$(".mouse-trap").bind('mouseenter', function() {
    // mouse came in

    $(this).one('mouseleave', function() {
        // mouse left
    });
});

In the first scenario if you have n events, this means jQuery is constantly have to listen for n x 2 events.

In the second scenario, assuming the div’s aren’t nested inside each other, you’re only listening for n + 1 events at any given point.

Admittedly I’m not sure exactly how much time is involved with event lookups, but I’d suspect from browser to browser, and computer to computer it would be different. For a relatively small value of n this would probably never be an issue. But what if n was really large? Would there be a noticeable difference?


Now, how do things change if you can have divs nested inside of each other?

For example:

<div class="mouse-trap">
    <div class="mouse-trap">
        <div class="mouse-trap">
        ...
        </div>
        <div class="mouse-trap">
        ...
        </div>
    </div>
    ...
    <div class="mouse-trap">
        ...
    </div>
</div>

The worst case scenario here would be n divs nested inside each other.

Because they are nested, I believe the “mouseleave” event won’t get called on the outer divs as you move inwards. In this case I believe both scenarios would have to keep track of n x 2 events, but only when mousing over the inner-most div.


Now this brings up another question of how much overhead is required to bind and unbind events. I thought I read a while back that jQuery keeps all events in a queue, although I wasn’t able to find specific documentation confirming that when researching this issue. So depending on the data structure used, the queuing/dequeuing probably has either a O(1) or O(n) cost. Does anybody know the specifics of this?

In scenario 1, you bind everything once up front and you don’t have to mess with it anymore (barring new div.mouse-traps getting dynamically added to the page). That’s n x 2 bindings that take place.

In scenario 2, you only bind n events up front, but there could theoretically be no limit to how many times you enter and leave an area with your mouse. These creates a possibility for infinite bindings necessary. But in the same respect, a mouse (and the human that operates that mouse) can only move so quickly, so these bindings are also all spread out over a greater period of time. This could minimize the effect of the numerous bindings.


All of that to say, I think the second scenario is probably better, but am I overlooking something else? Are there other ways of doing it that are better than the two that I listed?

I apologize for the lengthiness of this post. I just wanted to make sure I elaborated on my current thought process. I’m working on something that will have a large number of mouse events and want to make sure I play nicely with people’s systems.

  • 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-23T21:53:43+00:00Added an answer on May 23, 2026 at 9:53 pm

    Wrap all .mouse-trap in div, bind the events only once on that and use event.target to handle it.


    As a side note – you should profile it and see which solution it the best for you.

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

Sidebar

Related Questions

I want to trigger a hover event for an element using jQuery, but I
I'm using jQuery to develop in web environment. I want to know why $(#a#trigger).trigger('mouseenter');
I am using jquery ui datepicker icon trigger. I want to give the icon
is there a way to know if the mouse event has left from a
Is it possible to trigger a mouseout event on a link element using jQuery
I want to trigger javascript alert using PHP. Is it possible I want to
I have a div, containing text and a few links. I want to trigger
I know that jQuery can programmatically trigger events on DOM elements that are listening
Stack Fans, I have a div, that as you mouse off it, I want
I want to know if it is possible to add properties to the event

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.