Skip to content

Instantly share code, notes, and snippets.

@bensmithett
Created February 22, 2012 23:27
Show Gist options
  • Save bensmithett/1888342 to your computer and use it in GitHub Desktop.
Save bensmithett/1888342 to your computer and use it in GitHub Desktop.
jQuery event maps with delegation?
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