Skip to Content
jQuery Pocket Reference
book

jQuery Pocket Reference

by David Flanagan
December 2010
Intermediate to advanced
160 pages
3h 31m
English
O'Reilly Media, Inc.
Content preview from jQuery Pocket Reference

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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Beyond jQuery

Beyond jQuery

Ray Nicholus
jQuery Cookbook

jQuery Cookbook

Cody Lindley
Head First jQuery

Head First jQuery

Ryan Benedetti, Ronan Cranley

Publisher Resources

ISBN: 9781449398958Errata Page