JavaScript Eventlisteners in DataTables refuse to work

I fail to make a checkbox do a simple alert like I try here: https://jsfiddle.net/2n3dyLhh/5/ The problem is datatables (or how I use it) because when I do not include the library the listeners work perfectly.

The checkbox is embedded in a databales colum. Why does this fail?

<table id="eventsTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Name</th>
        <th>Checkbox
            <input type="checkbox" id="checkall" />
        </th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Tiger Nixon</td>
        <td>
            <input type="checkbox" id="checkbox1" name="checkbox1"/>
        </td>
    </tr>
    <tr>
        <td>Garrett Winters</td>
        <td>
            <input type="checkbox" id="checkbox2" />
        </td>
    </tr>
    <tr>
        <td>Ashton Cox</td>
        <td>
            <input type="checkbox" id="checkbox3" />
        </td>
    </tr>
    <tr>
        <td>Cedric Kelly</td>
        <td>
            <input type="checkbox" id="checkbox4" />
        </td>
    </tr>
    <tr>
        <td>Airi Satou</td>
        <td>
            <input type="checkbox" id="checkbox5" />
        </td>
    </tr>
    <tr>
        <td>Brielle Williamson</td>
        <td>
            <input type="checkbox" id="checkbox6" />
        </td>
    </tr>
    <tr>
        <td>Herrod Chandler</td>
        <td>
            <input type="checkbox" id="checkbox7" />
        </td>
    </tr>
    <tr>
        <td>Rhona Davidson</td>
        <td>
            <input type="checkbox" id="checkbox8" />
        </td>
    </tr>
    <tr>
        <td>Colleen Hurst</td>
        <td>
            <input type="checkbox" id="checkbox9" />
        </td>
    </tr>
    <tr>
        <td>Sonya Frost</td>
        <td>
            <input type="checkbox" id="checkbox10" />
        </td>
    </tr>
    <tr>
        <td>Jena Gaines</td>
        <td>
            <input type="checkbox" id="checkbox11" />
        </td>
    </tr>
    <tr>
        <td>Quinn Flynn</td>
        <td>
            <input type="checkbox" id="checkbox12" />
        </td>
    </tr>
</tbody>

$(document).ready(function () {
$.extend($.fn.dataTable.defaults, {
    "columns": [null, {
        "orderable": false
    }]
});
$('#eventsTable').DataTable();

});
$("#checkall").on('click', function () {
     $('#eventsTable').DataTable()
        .column(1)
        .nodes()
        .to$()
        .find('input[type=checkbox]')
        .prop('checked', this.checked);
});

$("#checkbox12").on('click', function () {
     alert('it works');
});
$("#checkbox1").on('click', function () {
     alert('it works');
});
alert('one can do alerts in jfiddle');
Answer:1

Some of the click handlers does not work because some of the rows are hidden by dataTables at the time you are assigning the handlers.

So you must use a delegated handler that assign click handlers to the checkboxes as they appear in the DOM. Besides that, you dont have to write a click handler for each and every checkbox; you can reduce it to one handler only and in that handler do whatever you want according to what checkbox that was clicked.

$("#eventsTable").on('click', '[type="checkbox"]', function () {
   alert('checkbox #'+$(this).attr('id')+' clicked'); 
});

forked fiddle -> https://jsfiddle.net/s4z01p0z/

Answer:2

I'm using fancyBox v2.1.4. In Chrome it's allowing scrolling of the main page when the fancyBox is open. I'm utilizing the locked: true but that doesn't seem to solve the issue. I have also ...

I'm using fancyBox v2.1.4. In Chrome it's allowing scrolling of the main page when the fancyBox is open. I'm utilizing the locked: true but that doesn't seem to solve the issue. I have also ...

I'm using this code to prevent people from puting spaces between alphabets and numbers. I would lilke to modify the code so that only alphabets, letters and numbers can be typed. This is what i'm ...

I'm using this code to prevent people from puting spaces between alphabets and numbers. I would lilke to modify the code so that only alphabets, letters and numbers can be typed. This is what i'm ...

  1. regex javascript allow spaces
  2. regex javascript allow special characters
  3. regex javascript allow only numbers
  4. javascript regex allow only alphanumeric and spaces
  5. javascript regex allow only numbers and dot
  6. javascript regex allow only one dot
  7. javascript regex allow empty string
  8. javascript regex allow only alphanumeric
  9. javascript regex allow dash
  10. javascript regex allow only letters and numbers
  11. javascript regex allow numbers and decimals only
  12. javascript regex allow numbers and dashes
  13. javascript regex allow whitespace
  14. javascript regex allow negative numbers
  15. javascript regex allow only letters
  16. javascript regex allow hyphen
  17. javascript regex allow some special characters
  18. javascript regex allow only one space
  19. javascript regex allow backslash
  20. javascript regex allow parentheses

I'm trying to submit a form with AJAX, the form being content of jQuery UI Dialog. I use the same code as in jQuery's documentation but I modified the form so there is only one text input, so the ...

I'm trying to submit a form with AJAX, the form being content of jQuery UI Dialog. I use the same code as in jQuery's documentation but I modified the form so there is only one text input, so the ...

In ECMAScript 6, I have two way to define a dictionary: Traditional method (with dictionary = Object.create(null)): set property: dictionary.key = value or dictionary[key] = value get property: ...

In ECMAScript 6, I have two way to define a dictionary: Traditional method (with dictionary = Object.create(null)): set property: dictionary.key = value or dictionary[key] = value get property: ...