Possible Duplicate:
jquery on vs click methods
From API:
.on() : Attach an event handler function for one or more events to the selected elements.
Example code:
$("#dataTable tbody tr").on("click", function(event){
alert($(this).text());
});
We can just write something like:
$("#dataTable tbody tr").click(function() {
alert($this).text();
});
Why bother wrapping the event inside .on() ?
Thanks.
Why bother wrapping the event inside .on() ?
in this specific example, using
on()you could take benefit ofevent delegation, so instead of attaching a lot of handlers for everytrelement you could simply doFurthermore, using
on()you could assign anamespaceto your events, e.g.So you could detach later your events in a cleaner and more refined way, e.g.
while
click(function()...)is just a shorthand method for.on("click", function()...)