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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:14:14+00:00 2026-05-23T14:14:14+00:00

I am trying to register a event on elements like this: $(document).ready(function() { $(‘.classname’).change(function(){

  • 0

I am trying to register a event on elements like this:

$(document).ready(function() {
   $('.classname').change(function(){
       alert ('This line is never triggered');
   });
});

But the problem is that .classname elements are later loaded into the dom by ajax. So, how do I correctly register an event in this case? Is it possible to do it only once (I mean, not every time the new element appears?)

  • 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-23T14:14:14+00:00Added an answer on May 23, 2026 at 2:14 pm

    Further to the answers suggesting that you use live(), it’s also possible to use delegate(), for example:

    $('form').delegate('.classname', 'change', function(){
        alert("This will alert when the newly-added element registers a 'change' event.");
    });
    

    For delegate the element to assign the selector (.classname') to, in this case the $('form') must exist in the DOM at the time of assignment.


    Edited to note that the following section (following the next ‘edited’ until the ‘Reference’) is wrong:

    JS Fiddle demo.

    Calling stopPropagation() on an element between the element selected by the delegate() selector and the target to which the event is delegate()-d does somewhat predictably (though I hadn’t realised this before) prevent the delegation from occurring.

    Edited in response to question, in comments, from OP:

    Hey, just noticed, that if any parent’s event handler calls stopPropagation, then live event would stop! Is this correct?

    Not according to the jQueryAPI (the entry is linked-to below):

    …events handled by .delegate() will always propagate to the element to which they are delegated…

    I’ve not tried/verified this as yet, but it seems that even if an element that sits between the target element ($('.classname')) and the delegate()-d element ($('form')) calls stopPropagation() it shouldn’t have an adverse effect on the delegate().

    Reference:

    • delegate().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to register the NSMouseMoved event globally using this line of code: [NSEvent
I am trying to register a WebDeleting event receiver within SharePoint. This works fine
I'm currently trying to register a JS function call from a .NET page which
I am trying to figure out the syntax to register a callback with this
I'm trying to create a feature to register my event handler to a specific
I was trying to understand c++0x std::bind and std::function usage in event handling /
I have a class which trying to register an event in another class. In
I'm now trying to register a mouse event listener on a canvas element. As
I am trying to change a value in the event arguments. Not sure if
I'm trying to register an externally hosted SQL 2000 server through Enterprise Manager which

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.