Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you do this:</p> <pre><code>$('#edit-field-project-dnr-und-0-remove-button').click(function (){ calculateDonorSum(); }); </code></pre> <p>This searches the current DOM for any element that has an <code>id="edit-field-project-dnr-und-0-remove-button"</code> and attaches an event handler directly to that DOM element.</p> <p>If you remove that DOM element and create some new DOM element or add a new DOM element, that new DOM element will NOT have this event handler attached to it unless you run some new code to attach an event handler to the new element.</p> <p>For dynamic elements, it is also possible to use delegated event handling, but you haven't really described enough of what you're doing for us to know how to recommend that. I can't tell if you're adding a new button or changing the ID on the current button.</p> <p>If you are adding a new button and want all new buttons of this type to have this event handler, then you can use delegated event handling. Delegated event handling works like this:</p> <pre><code>$("some static common parent selector").on("click", "some common child selector", fn); </code></pre> <p>So, if your buttons were all in a <code>id="container"</code> div and all had a common class name on them <code>class="calcButton"</code>, then you could use:</p> <pre><code>$("#container").on("click", ".calcButton", function() { calculateDonorSum(); }); </code></pre> <p>And, all buttons in the container with that class would have this event handler, even if they are dynamically created after the event handler is defined.</p> <p>Some other references on 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/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> <p><a href="https://stackoverflow.com/questions/12824549/should-all-jquery-events-be-bound-to-document/12824698#12824698">Should all jquery events be bound to $(document)?</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>
 

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