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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:54:25+00:00 2026-06-05T05:54:25+00:00

I used to assign an individual event handler to each element of a given

  • 0

I used to assign an individual event handler to each element of a given class at the time of its creation by AJAX, like so:

$('#newId').on('click', function (e) {...

I’ve changed to have one handler for the class that applies to all elements, like this:

$('#container').on('click', '.myClass', function (e) {...

I’ve been wondering about how exactly this works when the handler is fired, and what the performance implications might be. More specifically,…

How exactly does the element find the class handler? Is this slower than having one handler per element? And is there really only one event handler for the class, or does it get reproduced somehow for each element in the class?

  • 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-05T05:54:27+00:00Added an answer on June 5, 2026 at 5:54 am

    As mentioned in a comment, there are benefits and drawbacks to each approach.

    Event delegation is usually good when you have a container with child elements coming and going. Think of a large unordered list with edit/delete buttons. When a delete button is clicked, you’ll more than likely be deleting it’s parent li. Without event delegation, you’d be responsible for unbinding any event handlers bound on those elements.

    Event delegation uses the event bubbling properties of the DOM. When the click event is triggered on a child of #container, that event will then bubble up the node tree searching for handlers for the event along the way. The event target is then tested to see if it matches a selector. If so, your bound click handler is called. Without jQuery, this might look like…

    function myclass_click_handler() {
        // an element with myClass has been called
    }
    
    document.getElementById('container').addEventListener('click', function(e) {
        if (e.target.className.indexOf('myClass') > -1) {
            myclass_click_handler();
        }
    });
    

    One of the drawbacks of event delegation is that it doesn’t work for all events. From a performance perspective, it can actually improve performance because there can be less of a chance for memory leaks.

    When assigning a callback directly to elements based on class, each element will get a reference to your callback function. If you needed to then remove any number of those elements, you’d need to unbind the event handlers for those elements. jQuery helps out with this when you use the remove() method by removing bound event handlers.

    Hope this helps!

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

Sidebar

Related Questions

In symfony's routing.yml, you can assign a different class to be used in place
Intersect can be used to find matches between two collections, like so: // Assign
In the following code DEFAULT_CACHE_SIZE is declared later, but it is used to assign
I need to assign a variable which will be used to create the id
Where have you mostly used the initialization block? Can you use these to assign
I wanted to assign null value to DateTime type in c#, so I used:
If I'm not wrong this kind of declaration may be used to assign default
Is there any tag in JSF which can be used to assign some id
I used code from http://viralpatel.net/blogs/dynamic-add-textbox-input-button-radio-element-html-javascript/ html-javascript/ .my code is this: <HTML> <HEAD> <TITLE>Dynamically add
{assign var=tmp_url value=setURI(array('page' => 1))} It seems array() can't be used in smarty ?

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.