Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you are adding objects to the page asychronously via ajax and you want to hook up event handlers to some of those newly added objects, you have these options:</p> <ol> <li><p>You can use delegated event handling by assigning a delegated event handler to a static parent object that is present in the page at the time the page is originally loaded.</p></li> <li><p>You can add event handlers to the dynamically added objects in the success handler of the ajax call that added them after the objects have been added to the page.</p></li> <li><p>You can check for new objects in the page after all ajax calls (this is somewhat of a hack, but it sounds like what you're doing). You will have to be very careful to not add duplicate event handlers to objects that already existed before this ajax call.</p></li> </ol> <p>If the structure of your page HTML and classes is designed for it, then delegated event handling is often the simplest way to handle this. Here are a few previous answers that describe how to use delegated event handling:</p> <p><a href="https://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-event-after-loading-dynamic-ht/8752376#8752376">jQuery .live() vs .on() method for adding a click event after loading dynamic html</a></p> <p><a href="https://stackoverflow.com/questions/9730277/jquery-event-handlers-whats-the-best-method/9730309#9730309">JQuery Event Handlers - What&#39;s the &quot;Best&quot; method</a></p> <p><a href="https://stackoverflow.com/questions/9814298/does-jquery-on-work-for-elements-that-are-added-after-the-event-handler-is-cre/9814409#9814409">Does jQuery.on() work for elements that are added after the event handler is created?</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload