I’m used to using .click() and delegate('click'), so when I read both were deprecated in recent versions of jQuery I thought I’d read up on it, but I’m scratching my head a bit.
The documentation here seems to suggest that this is a drop-in replacement for .live() and .delegate(), but .click() and .bind() had a different behavior, namely binding to currently existing objects, where the others bound to any objects that matched the selector pattern througout the lifespan of the DOM.
In most cases, this wouldn’t make a big difference, but when adding elements to your DOM dynamically, this is an important distinction. New objects matching the old pattern would not have listeners tied to the click event using .click(), but would with .delegate().
My question is, how does one use the .on() method to duplicate the behavior of both the pre-existing .delegate() and .bind()? Or is everything in the future going towards the .delegate() style?
Both modes are still supported.
The following call to
bind():Can be directly converted into the following call to
on():And the following call to
delegate():Can be converted into the following call to
on():For completeness, the following call to
live():Can be converted into the following call to
on():UPDATE:
Except
onevent, the rest of the events were deprecated in different jQuery versions.bind– version deprecated: 3.0live– version deprecated: 1.7, removed: 1.9delegate– version deprecated: 3.0