32

I have a series of elements (lets call them '.my-elements') - some load on document ready, while others are loaded later via a pagination script.

I would like to set a variable according to whether or not the mouse is over these elements. The code below works, but I suspect there is a better way... Can I do this so I only have to reference the DOM once?

$(document).on('mouseenter','.my-elements', function(){
    mouse_is_inside = true;
});

$(document).on('mouseleave','.my-elements', function(){
    mouse_is_inside = false;
});

Thanks!

Nicole Harris
  • 928
  • 1
  • 12
  • 24

6 Answers6

97

You can bind to both together and check the event.type:

$(document).on('mouseenter mouseleave', '.my-elements', function (ev) {
    mouse_is_inside = ev.type === 'mouseenter';
});

Or, if you want to keep them separate, .on has another syntax that takes an event map:

$(document).on({
    mouseenter: function () {
        mouse_is_inside = true;
    },

    mouseleave: function () {
        mouse_is_inside = false;
    }
}, '.my-elements');
Jonathan Lonowski
  • 117,332
  • 31
  • 195
  • 197
8

Check out jQuery hover which is the same as:

$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

UPDATE: I just realized you need to persist the events via the on() method. In that case, you can use an event map like so:

.on({
    mouseenter: function() {
        console.log('enter');
    },
    mouseleave: function() {
        console.log('bye!');
    }
})
Derek Hunziker
  • 12,806
  • 3
  • 56
  • 103
6

Almost all jQuery methods return objects, so you can chain them together:

$(document).on('mouseenter','.my-elements', function(){
    mouse_is_inside = true;
}).on('mouseleave','.my-elements', function(){
    mouse_is_inside = false;
});
HellaMad
  • 5,171
  • 6
  • 30
  • 53
2

You could also try:

$(".my-elements").hover(function(eIn) {
    // do work for mouse over
}, 
function(eOut) {
    // do work for mouse out
});

update and correction

realized you need more dynamic lock in which case Jonathan Lonowski's or Derek Hunziker's is perfect

SpYk3HH
  • 21,961
  • 10
  • 67
  • 81
  • That will not work on the dynamically created elements. **Quote OP:** _"...some load on document ready, while others are loaded later..."_ – Sparky Mar 21 '12 at 20:17
1

For starters, you can select for your elements instead of document.

$('.my-elements').on('mouseenter', function(){
    mouse_is_inside = true;
});

You could try a shortcut notation like this...

$('.my-elements').on('mouseenter mouseleave', function(){
    mouse_is_inside = !mouse_is_inside;
});

This will negate the value every time the mouse goes in or out, which should keep the mouse_is_inside variable set to the right value.

Surreal Dreams
  • 25,215
  • 3
  • 43
  • 60
0
$('.my-elements').on('mouseenter mouseleave', function(event){
    mouse_is_inside = event.type === 'mouseenter';
});

but its generally not a good idea to have a global variable incidating a event state

worenga
  • 5,686
  • 2
  • 25
  • 47