The jQuery library lets you register events using the following methods: bind, live, delegate, on. Do you know when and where to use them? Do you know the difference between them?
If you don’t yet, we suggest reading this article.
BIND
| |
Registers events on the elements selected by the jQuery library. It only allows registration on elements initially present in the DOM, meaning that if you create elements using javascript, ajax.. bind won’t work.
See what the official jQuery documentation says about bind.
LIVE
| |
Registers events on the elements selected by the jQuery library, unlike bind (mentioned above), live lets you register events for elements that will be loaded through ajax or javascript. The downside of this approach is performance, this method is deprecated and its use is not recommended by the documentation.
See what the official jQuery documentation says about live.
DELEGATE
| |
The delegate method is similar to live (mentioned above), because elements created after the page loads also respond to events. The difference is that it doesn’t use document as the root element, you choose the root element yourself, matching it with the selector. So this method’s performance advantage is clear.
See what the official jQuery documentation says about delegate.
ON
| |
If you use a jQuery version higher than 1.7, you should always use the on method (it was introduced to replace all the other functions), for earlier versions the use of bind and delegate is recommended, as we’ve just seen.
See what the official jQuery documentation says about on.
That’s it folks, if you have any questions about using these methods or anything to add, leave a comment, we’ll be happy to reply.
REFERENCES