0

I want to select a checkbox in all rows in a DataTables list on all pages. This is my code:

    $('#selectAll').click(function (e) {
        var p = oTableAPI.rows('tr', { page: 'all', "filter": "applied" }).nodes();
        $.each(p, function (i, e) {
            e.find('input[type = checkbox]').prop('checked', this.checked);
        });
    });

On this row i get Uncaught TypeError: undefined is not a function:

e.find('input[type = checkbox]').prop('checked', this.checked);

What am i doing wrong?

1 Answer 1

2

e is a reference to the DOM element, but is not a jQuery element and therefore doesn't have the $.find method

use $(e) or $(this)

$(e).find('input[type = checkbox]').prop('checked', this.checked);

EDIT this.checked points to the current element in the each loop. maybe save a checked variable, or use var self = this;

$('#selectAll').click(function (e) {
    var checked = this.checked;
    var p = oTableAPI.rows('tr', { page: 'all', "filter": "applied" }).nodes();
    $.each(p, function () {
        $(this).find('input[type = checkbox]').prop('checked', checked);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

When i console.log($(e)) i get "[tr.odd, context: tr.odd, jquery: "2.1.1", constructor: function, selector: "", toArray: function…]", is this correct? The checkbox does not get checked however...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.