Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:51:31+00:00 2026-06-12T14:51:31+00:00

Where this is coming from When I first learned jQuery, I normally attached events

  • 0

Where this is coming from

When I first learned jQuery, I normally attached events like this:

$('.my-widget a').click(function() {
    $(this).toggleClass('active');
});

After learning more about selector speed and event delegation, I read in several places that “jQuery event delegation will make your code faster.” So I started to write code like this:

$('.my-widget').on('click','a',function() {
    $(this).toggleClass('active');
});

This was also the recommended way to replicate the behavior of the deprecated .live() event. Which is important to me since a lot of my sites dynamically add/remove widgets all the time. The above doesn’t behave exactly like .live() though, since only elements added to the already existing container ‘.my-widget’ will get the behavior. If I dynamically add another block of html after that code has ran, those elements will not get the events bound to them. Like this:

setTimeout(function() {
    $('body').append('<div class="my-widget"><a>Click does nothing</a></div>');
}, 1000);

What I want to achieve:

  1. the old behavior of .live() // meaning attaching events to not yet existent elements
  2. the benefits of .on()
  3. fastest performance to bind events
  4. Simple way to manage events

I now attach all events like this:

$(document).on('click.my-widget-namespace', '.my-widget a', function() {
    $(this).toggleClass('active');
});

Which seems to meet all my goals. (Yes it’s slower in IE for some reason, no idea why?)
It’s fast because only a single event is tied to a singular element and the secondary selector is only evaluated when the event occurs (please correct me if this is wrong here). The namespace is awesome since it makes it easier to toggle the event listener.

My Solution/Question

So I’m starting to think that jQuery events should always be bound to $(document).
Is there any reason why you would not want to do this?
Could this be considered a best practice? If not, why?

If you’ve read this whole thing, thank you. I appreciate any/all feedback/insights.

Assumptions:

  1. Using jQuery that supports .on() // at least version 1.7
  2. You want the the event to be added to dynamically added content

Readings/Examples:

  1. http://24ways.org/2011/your-jquery-now-with-less-suck
  2. http://brandonaaron.net/blog/2010/03/4/event-delegation-with-jquery
  3. http://www.jasonbuckboyer.com/playground/speed/speed.html
  4. http://api.jquery.com/on/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:51:32+00:00Added an answer on June 12, 2026 at 2:51 pm

    No – you should NOT bind all delegated event handlers to the document object. That is probably the worst performing scenario you could create.

    First off, event delegation does not always make your code faster. In some cases, it’s is advantageous and in some cases not. You should use event delegation when you actually need event delegation and when you benefit from it. Otherwise, you should bind event handlers directly to the objects where the event happens as this will generally be more efficient.

    Second off, you should NOT bind all delegated events at the document level. This is exactly why .live() was deprecated because this is very inefficient when you have lots of events bound this way. For delegated event handling it is MUCH more efficient to bind them to the closest parent that is not dynamic.

    Third off, not all events work or all problems can be solved with delegation. For example, if you want to intercept key events on an input control and block invalid keys from being entered into the input control, you cannot do that with delegated event handling because by the time the event bubbles up to the delegated handler, it has already been processed by the input control and it’s too late to influence that behavior.

    Here are times when event delegation is required or advantageous:

    • When the objects you are capturing events on are dynamically created/removed and you still want to capture events on them without having to explicitly rebind event handlers every time you create a new one.
    • When you have lots of objects that all want the exact same event handler (where lots is at least hundreds). In this case, it may be more efficient at setup time to bind one delegated event handler rather than hundreds or more direct event handlers. Note, delegated event handling is always less efficient at run-time than direct event handlers.
    • When you’re trying to capture (at a higher level in your document) events that occur on any element in the document.
    • When your design is explicitly using event bubbling and stopPropagation() to solve some problem or feature in your page.

    To understand this a little more, one needs to understand how jQuery delegated event handlers work. When you call something like this:

    $("#myParent").on('click', 'button.actionButton', myFn);
    

    It installs a generic jQuery event handler on the #myParent object. When a click event bubbles up to this delegated event handler, jQuery has to go through the list of delegated event handlers attached to this object and see if the originating element for the event matches any of the selectors in the delegated event handlers.

    Because selectors can be fairly involved, this means that jQuery has to parse each selector and then compare it to the characteristics of the original event target to see if it matches each selector. This is not a cheap operation. It’s no big deal if there is only one of them, but if you put all your selectors on the document object and there were hundreds of selectors to compare to every single bubbled event, this can seriously start to hobble event handling performance.

    For this reason, you want to set up your delegated event handlers so a delegated event handler is as close to the target object as practical. This means that fewer events will bubble through each delegated event handler, thus improving the performance. Putting all delegated events on the document object is the worst possible performance because all bubbled events have to go through all delegated event handlers and get evaluated against all possible delegated event selectors. This is exactly why .live() is deprecated because this is what .live() did and it proved to be very inefficient.


    So, to achieve optimized performance:

    1. Only use delegated event handling when it actually provides a feature you need or increases performance. Don’t just always use it because it’s easy because when you don’t actually need it. It actually performs worse at event dispatch time than direct event binding.
    2. Attach delegated event handlers to the nearest parent to the source of the event as possible. If you are using delegated event handling because you have dynamic elements that you want to capture events for, then select the closest parent that is not itself dynamic.
    3. Use easy-to-evaluate selectors for delegated event handlers. If you followed how delegated event handling works, you will understand that a delegated event handler has to be compared to lots of objects lots of times so picking as efficient a selector as possible or adding simple classes to your objects so simpler selectors can be used will increase the performance of delegated event handling.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Coming from NHibernate, I've tried to do something like this in Java (first example):
I have a view model coming from the server as json like this {
(WARNING: this is my first java application, coming from .NET, so don't bash me
I have this data coming from a REST method using jquery's getJSON method. [Date.UTC(2010,0,0,0,0,0,0),
Introduction I have a question coming from this one: Loop calling an asynchronous function
This is my first attempt at understanding Linq to Entity coming from Net Tiers.
I have data in this format coming from a database... BUS 101S Business and
I'm seeing this exception message coming from XslCompiledTransform.Transform(), but after handling the exception the
I have an image byte[] , this byte[] is coming from LDAP [Oracle Open
This is the json coming from the server: { name:channelname, args: [ { username:myusername,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.