And why is .on() now preferred in jQuery 1.7?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
.on()now offers a combination of.live(),.delegate()and.bind()all in one unified method. You can get the behavior of any of those three just by how you use the arguments to.on().These pairs are functionally the same:
More info is described in a jQuery blog entry.
Before unifying these separate functions, jQuery had multiple different implementations. Now,
.on()is the superset function and.bind(),.live()and.delegate()all just call.on()in their implementation so there is now only one implementation of the actual event handling. So, from that standpoint, it was also a code cleanup and streamlining issue. Similarly,.die(),.undelegate()and.unbind()just call.off()now rather than have separate implementations.Note:
.live()has been deprecated for all versions of jQuery because it’s just a special case of intercepting all the bubbled events on the document object so it can be easily replaced with either.delegate()or.on()and when lots of events were all being handled on the document object, it could become a performance problem checking lots of selectors on every event. It’s much more efficient to hook a delegated event like this to a common parent that is much closer to where the event occurs than put them all on the document object (thus why.live()is not good to use).From the jQuery 1.7 source, you can see how all these functions just now call
.on()and.off():