Rohan Kumar
Rohan Kumar

Reputation: 40639

delegate() vs on() in jquery?

I know the difference between delegate() and on(), but I have a doubt that on() is very popular and faster than delegate() then why jquery is not removing the delegate() from its library.

live() has drawbacks then it is removed from jQuery-1.9 version. Also if we see the performance between on() and delegate() then on() is much faster than delegate().

Then any reason to keep delegate() in jQuery?

Upvotes: 4

Views: 543

Answers (5)

dsgriffin
dsgriffin

Reputation: 68576

This is what a member of the jQuery team wrote in regard to this issue in 2013:

"We haven't deprecated .bind or .delegate, nor have we indicated we're removing them at any point.

They're each only one line in the source so size reduction isn't much of a reason to remove them.

We deprecated .live because it has lots of confusing issues that we can't fix."

Basically, while delegate() has been superseded by on(), there are no future plans to deprecate delegate() due to the fact it takes up very little space (and doesn't cause any problems).

Upvotes: 3

Abhinav
Abhinav

Reputation: 8168

You can perform both event binding and delegation with the help of on() method. Just the syntax differs.

Just visit this link:

http://learn.jquery.com/events/handling-events/

They have beautifully explained with examples

Upvotes: 2

Alok Swain
Alok Swain

Reputation: 6519

on is meant to replace delegate. Probably the jquery authors might keep it sometime long to make it more backward compatible. Many old plugins still use those methods. probably that might be the reason to keep it.

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

I think it is more to do with whether they want to break the users who has used it.... in terms of delegated handlers I don't see any performance diff between them...

So as advised new code should use .on() not .delegate

Upvotes: 2

Quentin
Quentin

Reputation: 943207

From the documentation:

As of jQuery 1.7, .delegate() has been superseded by the .on() method.

It is just legacy code. Don't use it.

Upvotes: 3

Related Questions