Advanced Event Handler Registration
We’ve seen that jQuery defines quite a few simple methods for
registering event handlers. Each of these simply invokes the single,
more complex method bind() to bind a
handler for a named event type to each of the elements in the jQuery
object. Using bind() directly allows
you to use advanced event registration features that are not available
through the simpler methods.[2]
In its simplest form, bind()
expects an event type string as its first argument and an event handler
function as its second. The simple event registration methods use this
form of bind(). The call $('p').click(f), for example, is equivalent
to:
$('p').bind('click', f);
bind() can also be invoked with
three arguments. In this form, the event type is the first argument and
the handler function is the third. You can pass any value between those
two and jQuery will set the data
property of the Event object to the value you specify before it invokes
the handler. It is sometimes useful to pass additional data to your
handlers in this way without having to use closures.
There are other advanced features of bind() as well. If the first argument is a
space-separated list of event types, the handler function will be
registered for each of the named event types. The call $('a').hover(f) (see Simple Event Handler Registration), for example, is the same as:
$('a').bind('mouseenter mouseleave', f);
Another important feature of bind() is that it allows you to specify a namespace (or namespaces) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access