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

Delegating events


Someone once said that the art of being a good manager is to know when to delegate. I hope that this wasn't an excuse for them to offload a horrible job to a subordinate, although the cynical might say otherwise!

Leaving aside the risk, delegation follows the same principles in jQuery. If we need to create an application which requires binding some form of event handler to lots of elements of the same type, then we might consider writing event handlers to cover each element.

It'll work to an extent, but is very wasteful of resources. If the list is large, then events will be bound to all of the elements within, which uses more memory than is needed. We can get around this by using event delegation, where we can shift to binding one event handler to a single ancestor element that serves multiple descendants, or enable event handling for newly created elements.

There are a few tricks we can use to help us with better managing of events using delegation. Before we take a look...