Sunil
Sunil

Reputation: 21396

Iterate over all rows of a table in jQuery knowing the table element

I am getting a table element for a given row in the table using the following code in jQuery 1.8.2.

var tbl = $("input[id$='chkSelectAll']").closest('table');

Now, I want to replace the JavaScript code below with corresponding jQuery script. What would be the jQuery for this code below?

It would be nice if I could just get the table rows as an array from the above tbl variable in jQuery, as that would solve my problem.

    for (var i = 1;i < tbl.rows.length - 1;i++) {
         var chk = tbl.rows[i].cells[0].firstChild; //we have a checkbox in first cell of every row
        chk.checked = chkAll.checked; 
    }

Upvotes: 0

Views: 151

Answers (1)

Michael B.
Michael B.

Reputation: 2809

 var rows = $('tr', tbl);
 for (var i = 1;i < rows.size() - 1; i++) {
     rows.eq(i).find('td:first').find('input:checkbox').prop('checked', true );
    }

You can use "chkAll.prop('checked')" instead of "true" assuming that you have chkAll as jQuery object of that checkbox

Upvotes: 1

Related Questions