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 6817327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:02:21+00:00 2026-05-26T21:02:21+00:00

What is the different in performance and the handling of these two different jQuery

  • 0

What is the different in performance and the handling of these two different jQuery statements:

  1. Number One:

    $('#selector1, #selector2, .class1').on('click', function () { 
         //stuff
    });
    
  2. Number Two:

    $(document).on('click', '#selector1, #selector2, .class1', function () { 
         //stuff
    });
    

I know that one does delegation and the other doesn’t.

But what does that mean?

Don’t both do some sort of action when you click on '#selector1, #selector2, .class1'?

In the end, isn’t it the same?

  • 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-05-26T21:02:21+00:00Added an answer on May 26, 2026 at 9:02 pm

    Number One will hook the click event on the elements that exist when you execute that statement. E.g., the handler is directly hooked to the actual elements that match when you execute that call.

    Number Two will hook the click event on the document, and on receipt of any clicks, will check to see if the element that was actually clicked matches any of the given selectors, and if so will fire the handler. This is event delegation rather than a direct hookup.

    So that means several things:

    1. Using Number Two, clicks on elements you add later will trigger the handler; with Number One they won’t.
    2. Only bubbling events like click work with Number Two (because it relies on the event bubbling up the DOM to the document level).
    3. If you use a delegated handler (Number Two) and some other code hooks the event on the actual element and then cancels propagation (bubbling) of the event, the delegated handler won’t see it.
    4. The delegated form (Number Two) has to match the element that was clicked (and potentially its ancestors) against the selector when the click occurs, which takes a non-zero amount of time. Not necessarily much time, but more than a direct handler (which doesn’t have to do that) would. If you have a lot of delegated handlers on the same element (in this case, the document), you might start noticing.

    There are times when using a directly-hooked handler is better, and times when event delegation (usually using something more focussed than the document, though) is better. Usually, the dividing line between those is a judgement call, but for example if you want to respond to clicks on table rows, you’re probably better off hooking the click event on the table element with a tr selector, rather than attaching the click event of every single table row, particularly if you update the table dynamically. Whereas if you have a unique button you know exists in the DOM when you’re hooking up your handler, and you want to fire a specific function when that button (but not anything else) is clicked, a direct handler probably makes more sense.

    Here’s an example (live copy):

    HTML:

    <p>Click me</p>
    

    JavaScript:

    jQuery(function($) {
    
      $('p').on('click', function() {
        display("Directly-attached handler fired. Click this paragraph and note the difference in what happens compared to when you click the 'click me' paragraph.");
      });
      $(document).on('click', 'p', function() {
        display("Delegated handler fired.");
      });
    
      function display(msg) {
        $("<p>").html(msg).appendTo(document.body);
      }
    });
    

    Note that when you click the “click me” paragraph, you get two new paragraphs added to the document, one of them the result of the first on call, the other the result of the second. But note that if you click either of those two new paragraphs, you only see the handler from the second on call (the delegated one), not the first. That’s because those paragraphs didn’t exist when you hooked up the first handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any performance different between hosting your asp.net in mono on linux and
Does anyone know if there is an overview of the performance of different cache
We want to compare general performance (CPU, I/O, network, ...) of different JVMs for
Is there any performance difference in my application if I change the number of
I am having massive performance problems on a site running two versions of the
I wonder if ArrayList<SimpleObject> and ArrayList<ComplexObject> has different performance effect if we are concerning
Suppose I have these two tables: Invoice ------- iInvoiceID int PK not null dtCompleted
I know plenty about the different ways of handling parsing text for information. For
In other words, are the scenarios different performance-wise? A: for($i = 0; $i <
Is there any different between for(;;) and for(:) in terms of performance in Objective-C?

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.