O'Reilly logo

Mastering jQuery by Alex Libby

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

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 ...

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