What's the best way to delegate multiple events using .on()?
This is the syntax for one delegated event:
$(document).on('mouseeenter','.foo', function(){});
And here is the syntax for multiple events (not delegated):
$('.foo').on({
mouseenter: function(){
//stuff
},
mouseleave: function(){
//stuff
}
});
I was wondering if there was a more succinct way to do this other then:
$(document).on('mouseenter', '.foo', function(){})
.on('mouseleave', '.foo', function(){});
It's not a big deal if I have to do it that way, I'm more curious about it than anything.
This means that mouseleave is fired when the pointer has exited the element and all of its descendants, whereas mouseout is fired when the pointer leaves the element or leaves one of the element's descendants (even if the pointer is still within the element).
The mouseleave event occurs when the mouse pointer leaves the selected element. The mouseleave() method triggers the mouseleave event, or attaches a function to run when a mouseleave event occurs. Note: Unlike the mouseout event, the mouseleave event only triggers when the mouse pointer leaves the selected elements.
Definition and Usage The mouseenter event occurs when the mouse pointer is over (enters) the selected element. The mouseenter() method triggers the mouseenter event, or attaches a function to run when a mouseenter event occurs..
jQuery mouseenter() This function is executed, when the mouse pointer enters the HTML element. When you enter your mouse cursor over the selected element, it triggers the mouseenter event and once the mouseenter event is occurred, it executes the mouseenter() method to attach the event handler function to run.
$(document).on({
mouseenter: function(){
//stuff
},
mouseleave: function(){
//stuff
}
}, '.foo');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With