jQuery in Action
The jQuery Event Model
103
These commands give us many choices to bind an event handler to matched ele-
ments. And once a handler is bound, we may eventually need to remove it.
ments. And once a handler is bound, we may eventually need to remove it.
4.2.2 Removing event handlers
Typically, once an event handler is established, it remains in effect for the remainder
of the life of the page. Particular interactions may dictate that handlers be removed
based on certain criteria. Consider, for example, a page where multiple steps are pre-
sented, and once a step has been completed, its controls revert to read-only.
of the life of the page. Particular interactions may dictate that handlers be removed
based on certain criteria. Consider, for example, a page where multiple steps are pre-
sented, and once a step has been completed, its controls revert to read-only.
For such cases, it would be advantageous to remove event handlers under
script control. We've seen that the
one()
command can automatically remove a
handler after it has completed its first (and only) execution, but for the more gen-
eral case where we'd like to remove event handlers under our own control, jQuery
provides the
eral case where we'd like to remove event handlers under our own control, jQuery
provides the
unbind()
command.
The syntax of
unbind()
is as follows:
This command can be used to remove event handlers from the elements of the
matched set at various levels of granularity. All listeners can be removed by omit-
ting parameters, or listeners of a specific type can be removed by providing that
event type.
matched set at various levels of granularity. All listeners can be removed by omit-
ting parameters, or listeners of a specific type can be removed by providing that
event type.
Specific handlers can be removed by providing a reference to the function orig-
inally established as the listener. For this to be possible, a reference to the function
must be retained when binding the function as an event listener in the first place.
For this reason, when a function that's eventually to be removed as a handler is
must be retained when binding the function as an event listener in the first place.
For this reason, when a function that's eventually to be removed as a handler is
Command syntax: unbind
unbind(eventType,listener)
unbind(event)
Removes events handlers from all elements of the wrapped set as specified by the optional
passed parameters. If no parameters are provided, all listeners are removed from the ele-
ments.
passed parameters. If no parameters are provided, all listeners are removed from the ele-
ments.
Parameters
eventType
(String) If provided, specifies that only listeners established for the specified
event type are to be removed.
event type are to be removed.
listener
(Function) If provided, identifies the specific listener that's to be removed.
event
(Event) Removes the listener that triggered the event described by this Event
instance.
instance.
Returns
The wrapped set.
The wrapped set.