Book Image

Mastering jQuery

By : Alex Libby
Book Image

Mastering jQuery

By: Alex Libby

Overview of this book

<p>Mastering jQuery has been written not only to help maximize your skills with core functionality in the library, but also to explore some of the more intriguing ways of using the library to achieve real-world solutions that could feature on any website or online environment.</p> <p>You'll start with a look at some of the more advanced ways to incorporate the library into your pages, followed by working with forms and advanced form validation using regular expressions. Next you'll move on to animating in jQuery, advanced event handling, and using jQuery effects.</p> <p>Finally, you will develop practical examples of using jQuery with external functionality such as node-webkit, before finishing with a session on optimizing your version of the library for maximum efficiency and exploring best practices for using QUnit.</p>
Table of Contents (21 chapters)
Mastering jQuery
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Namespacing events


So far, we've seen how we can delegate events and create handlers that can take custom triggers. These methods are perfect if we have a single click event handler, but what happens if we need to have multiple click handlers, for example?

Well, fortunately there's a simple solution: add a namespace to the event! Rather than talk about how it works, let's take a quick look at the following example:

$("#element")
  .on("click", doSomething)
  .on("click", doSomethingElse);

This code is perfectly acceptable – nothing wrong with this at all. Sure, it might not be quite as readable as some might like, but we're not worried about that – at least not for now!

The critical point here is if we were to call:

$("#element").off("click");

Then we would lose not only the first click handler, but the second one as well. This is not ideal. We can fix this by adding a namespace or identifier to the command, as shown next:

$("#element")
  .on("click.firsthandler", doSomething)
  .on("click.secondhandler...