0

I have a form which contains a set of checkboxes where multiple selection is possible. (Code snippets below).

The form's inner HTML is added by AJAX due to the time it takes to generate the contents, so the user initially sees a blank form and a few seconds later, its contents.

The problem is that the checkboxes need an onchange() event. For static fields that's easy - I would probably give them all the same class, then use jQuery $(".mychkboxes").on("change", my_event_handler);

For dynamically added fields I gather from other questions I would need to use event delegation, which I'm not so confident about, so I've added an onchange="my_event_handler();" to the field definition in the AJAX handler, and of course the handler function already exists in the web page in the browser when the form is populated.

But what's happening is that if I do "onchange=\"alert('test'); my_event_handler();\" ", the alert fires, so I know it's working, but my_event_handler() is never called although it exists and wasn't added programmatically.

I can't figure out why. Alternatively how would I add the handler programmatically in jQuery, so I can compare the 2 approaches.

Relevant Javascript (using events.push due to other reasons, in case relevant):

<script type="text/javascript">
//<![CDATA[
events.push(function() {

    function my_event_handler() {
        alert("handler entered");
    }

    ... ajax success handler relevant line...
    $('#myform_div').html(data.form_html);

});
//]]>
</script>

data.form_html includes this snippet (obviously the handler call here wouldn't be needed if done programmatically via delegation):

<div id="mydiv">
<input class="chk_mycheckboxlist" name="chk_mycheckboxlist[]" onchange="alert('event triggered'); my_event_handler();" id="chk_mycheckboxlist_item998" type="checkbox" value="998">

...

<input class="chk_mycheckboxlist" name="chk_mycheckboxlist[]" onchange="alert('event triggered'); my_event_handler();" id="chk_mycheckboxlist_item999" type="checkbox" value="999">
</div>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stilez
  • 558
  • 5
  • 14
  • `.on()` event binding should also work for dynamic html elements – codePG Jan 06 '17 at 08:36
  • Seen something earlier of this nature `$(div).on("change", ".chk_mycheckboxlist", function() { ..... });` by Jai [here](http://stackoverflow.com/a/41499118/3504007) – Kraang Prime Jan 06 '17 at 08:36
  • As you suspect, event delegation is exactly what you need here; and it's not complicated at all. You simply delegate the event to a parent element, like this: `$('#myform_div').on('change', 'chk_mycheckboxlist', function() { /* your code here */ ");`. See the question I marked as a duplicate for more information – Rory McCrossan Jan 06 '17 at 08:40
  • The reason for your issue is because when calling a function from an `on*` attribute it needs to be available in the scope of the `window`, where yours seem to be defined as part of an array or object. Event delegation is a far cleaner solution in either case – Rory McCrossan Jan 06 '17 at 08:42

0 Answers0