- Using
$.on()
, even if you are not using delegation, is good because it introduces familiar and consistent syntax into your code. - In addition, it was heavily used in the jQuery source, that is, you can cut out the average person when calling methods like
$.bind()
. - Using
$.submit()
or $.click()
to assign handlers is confusing, as it is also used to trigger these events.
Then the benefit of event delegation is added, which can save you from adding events to hundreds of elements when you only need to add them to one. Suppose you have 100 <td>
and would like to alert their contents on click. You can do it:
$("td").click(function(){ alert( $(this).text() ); });
But now you have added 100 new event handlers. Really bad for performance. Using $.on()
, you can only bind to <table>
and examine all the events to see what boosted them. If it was a <td>
, you can respond accordingly:
$("table").on("click", "td", function(){ alert( $(this).text() ); });
Voila, only one event was associated, and only one event handler was added to the application. Massive increase in productivity.
This is one of the main reasons why $.on()
trumps $.click()
or $.submit()
any day.
source share