Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespacing multiple events using jquery "on" [closed]

Wondering what the correct method to namespace multiple events using jquery "on" ...

for example:

$(".parent-selector").on({
"mouseenter.namespace": function() {
    // mouseenter event
},
"mouseleave.namespace": function() {
    // mouseleave event
}
}, ".selector");

This is not working... If i remove the ".namespace" it does work.

Example of jquery on with working namespace:

$(".parent-selector").on("mouseenter.namespace", ".selector", function() { });

I understand I can do both the mouseenter/mouseleave events seprately ... just curious if there is a way to pass namespaces through the object

Thanks!

like image 797
Roi Avatar asked Jan 18 '13 22:01

Roi


2 Answers

To attach to multiple events you have to pass a space separated list of events:

$(".parent-selector").on("mouseenter.namespace mouseleave.namespace",
                         ".selector", function() { });

Edit:

You can always get the type of event inside the callback and call other functions inside:

$(".parent-selector").on("mouseenter.namespace mouseleave.namespace", ".selector", function(e) {
    if (e.type == "mouseenter.namespace") {
        myMouseEnter(e);
    } else if (e.type == "mouseleave.namespace") {
        myMouseLeave(e);
    }
});

Althrough it seems to work, I can't confirm that, cause I'm not on my machine. Give it a try.

like image 153
Ricardo Souza Avatar answered Nov 11 '22 21:11

Ricardo Souza


The Below code is actually correct -- I had an issue with namespacing conflict earlier in the code.

$(".parent-selector").on({
    "mouseenter.namespace": function() {
        // mouseenter event
    },
    "mouseleave.namespace": function() {
        // mouseleave event
    }
}, ".selector");
like image 24
Roi Avatar answered Nov 11 '22 22:11

Roi