On() has all the abilities of all other bindings. To bind to dynamic elements you can use on()
like this:
$(document).on('click', '#foo', function() {
//
});
Preferably you want to use a close static element instead of document
.
To quote from another post of mine regarding the many binding methods jQuery has on offer:
bind()
was added in 1.0, live()
in 1.3, delegate()
in 1.4.2 and
on()
in 1.7.
As of 1.7 on()
is the preferred use and live()
is deprecated and
not recommended at all. If you are using 1.3 use bind()
instead of
live()
and as of 1.4.2 use delegate()
instead of live()
and as of
1.7 use on()
instead of any of the others.
You can see the full post here, which also list the many drawbacks of live()
and why it should not be used any more in jQuery 1.7 or later.