O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding Mouse-Click-Handling Code

$("p").on("click", function (e){   $(this).html("You clicked here."); }); $("span").on("dblclick", function (e){   $(this).html("You double-clicked here."); });

To register a click handler in jQuery, you should use the .on("click") or .on("dblclick") method. Inside the handler function, you can perform whatever event functionality your application requires. It is easy to apply the click handler to multiple objects using jQuery selectors. For example, the following applies a click handler to all <div> elements:

$("div").on("click", function (e){   $(this).html("You clicked here."); });

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required