jQuery Update - as of version 1.7
Stop using .delegate(), .live() and .bind() and simply use all purpose .on() method
A more elegant way to attach event handler functions
Click on the yellow highlighted div's to see that particular example
function myHandler(event) {
alert(event.data.foo);
}
$("pre#1").on("click", {foo: "bar"}, myHandler)
$("pre#2").on("click", function(){
alert( $(this).text() );
});
//Example: Attach multiple event handlers simultaneously using a map
click: function(){
$(this).toggleClass("active");
},
mouseenter: function(){
$(this).addClass("inside");
},
mouseleave: function(){
$(this).removeClass("inside");
}
});
var count = 0;
$("body").on("click", "pre#4", function(){
$(this).after("<pre>Another pre! "+(++count)+"</pre>");
});
Non-clickable - Some additional examples using .on() method
Example: Cancel a form submit action and prevent the event from bubbling up by returning false:
$("form").on("submit", false)
Example: Cancel only the default action by using .preventDefault().
$("form").on("submit", function(event) {
event.preventDefault();
});
Example: Stop submit events from bubbling without preventing form submit, using .stopPropagation().
$("form").on("submit", function(event) {
event.stopPropagation();
});
