Created
February 22, 2012 23:27
-
-
Save bensmithett/1888342 to your computer and use it in GitHub Desktop.
jQuery event maps with delegation?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $container = $(".container"); | |
// Woo delegated event handlers (but I have to call on() twice) | |
$container.on("click", ".control1", function () { | |
// blah | |
}); | |
$container.on("mouseleave", ".control2", function () { | |
// blah | |
}); | |
// Woo event map (but how can I delegate the event handler as above?) | |
$container.on({ | |
click: function () { | |
// blah | |
}, | |
mouseleave: function () { | |
// blah | |
} | |
}); | |
// OK on second thought, this is easy to do with an if | |
$container.on({ | |
click: function ( event ) { | |
$t = $(event.target) | |
if ( $t.hasClass( '.control1' ) ) { | |
// blah | |
} | |
if ( $t.hasClass( '.control2' ) ) { | |
// blah | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment