Search code examples
jquerycheckboxonclickonchangeicheck

On change dynamically created iCheck checkbox in jQuery


I have a script that created a list along with a set of checkboxes next to the list items, specifically here:

$('<td>').html('<input type="checkbox" class="batchCheckboxTransfer" name="batch[]" value="">')

And I am successfully able to reinitialize them so they appear as they are supposed to.

However, I can't seem to get an action associated with changing these dynamically created elements to work.

At the moment, I am just trying to get this to work but cannot:

<script>
    $('.box-body').on('change', '.batchCheckboxTransfer', function(){
        alert('hello');
    }
</script>

Solution

  • Any event listener should be added right after an element is created and appended in DOM. It can't be a separate script. try putting $('.box-body').on('change',..... right after append.