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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:40:44+00:00 2026-06-04T19:40:44+00:00

Let’s say I have some of links that I want to add a javascript

  • 0

Let’s say I have some of links that I want to add a javascript action to:

<a class='test'>
<a class='test'>
<a class='test'>

When my page loads I give them all a click event:

$('.test').click(function(){
  alert("I've been clicked!")
});

but let’s say afterward I add another element and I want to give it the same event. I can’t do this:

$('.test').click(function(){
  alert("I've been clicked!")
});

because then the first 3 will have 2 events on them. What’s the best way to deal with this?

  • 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-04T19:40:45+00:00Added an answer on June 4, 2026 at 7:40 pm

    You can bind the $.on to a parent element that will always exist in dom like this.

    $(document).on('click','.test', function() {
                console.log('Test clicked');
            });
    

    Note that:
    You can replace document with any parent of the element that will always exist in dom, and the closer the parent the better.

    Simple event binding with click wont work as click bind the event handlers to elements that already exists in dom at the time of binding. And hence it doesn’t work for elements dynamically added to dom later through Ajax or jQuery. For that purpose you have to use event delegation. And you can use $.on for that purpose.

    Check documentation of $.on

    You can use $.live but $live is depreciated. use $.on instead. Equivalent syntax of $.on for $.live and $.delegate which does the same thing

    $(selector).live(events, data, handler);                // jQuery 1.3+
    $(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
    $(document).on(events, selector, data, handler);        // jQuery 1.7+
    

    In this case event handler will be bound to document. And events will be delegated to target element by jQuery in this case test class.

    UPDATE

    I would suggest you to use $.on for all event handling purposes as all other methods routes through $.on and $.off method under the hood.

    Check the definition of these functions from jQuery source v.1.7.2

    bind: function( types, data, fn ) {
        return this.on( types, null, data, fn );
    },
    unbind: function( types, fn ) {
        return this.off( types, null, fn );
    },
    
    live: function( types, data, fn ) {
        jQuery( this.context ).on( types, this.selector, data, fn );
        return this;
    },
    die: function( types, fn ) {
        jQuery( this.context ).off( types, this.selector || "**", fn );
        return this;
    },
    
    delegate: function( selector, types, data, fn ) {
        return this.on( types, selector, data, fn );
    },
    undelegate: function( selector, types, fn ) {
        // ( namespace ) or ( selector, types [, fn] )
        return arguments.length == 1? this.off( selector, "**" ) : this.off( types, selector, fn );
    } 
    

    And that is true for these convenient events handlers too

    blur focus focusin focusout load resize scroll unload click dblclick mousedown 
    mouseup mousemove mouseover mouseout mouseenter mouseleave change select 
    submit keydown keypress keyup error contextmenu
    

    You can see all methods are using $.on and $.off themselves. So using $.on you can at least save a function call though which isn’t that significant in most of the cases.

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

Sidebar

Related Questions

Let's say I have some service that talks over ZeroMQ sockets, and I want
Let me explain best with an example. Say you have node class that can
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a bunch of links that share a click event: <a
Let's say I have a Player class. It has a Move method that takes
Let's say you have a class, with certain properties, and that you tried your
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say I have multiple requirements for a password. The first is that the

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.