Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery 1.7 .on() method, why is it necessary?

I don't understand why this method exists....

http://api.jquery.com/on/

It seems to me you'd always just do $(el).click() instead of $(el).on('click', function...

like image 404
Webnet Avatar asked Nov 03 '11 22:11

Webnet


2 Answers

$(el).on gives the developers an unified way of binding events for any event types, direct and delegated events. It also allows you to bind more than one events at the same time by passing an object (2nd example in the documentation)

You also save a few keystrokes, for what it's worth.

like image 145
Jimmy Avatar answered Oct 15 '22 19:10

Jimmy


With .on() you can bind more than one event type at the same time, so you don't have to duplicate code or create a separate function if you want to do the same thing on multiple events.

like image 37
JJJ Avatar answered Oct 15 '22 17:10

JJJ