Reputation: 6029
I have a HTML table where the first column is a checkbox, I have a hyper link above the table which says "check all" if they click this I need to loop through each row in the table and check the checkbox assigned to that row, the table is build on page load "Databind".
This is my jQuery so far I'm calling it off the "Check All" link:
$('#HypSelectAll').click(function() {
var count = $('#gvPerformanceResult>tbody>tr').length; //Count the amount of rows in the HTML table
for (i = 0; i < count; i++) {
}
});
I'm now not sure how I can reference the gvPerformanceResult columns.... to find the checkbox which is called "chkExportToExcel" this table could have 1-25 rows. Can someone help me out of give me a starting point?
Upvotes: 1
Views: 4444
Reputation: 219938
No need to manually do the loop. Let jQuery do its thing:
$('#HypSelectAll').click(function () {
$('#gvPerformanceResult')
.find('input[type="checkbox"]')
.prop('checked', true);
});
If there are other checkboxes throughout the table, and you really must be that specific, try this:
$('#HypSelectAll').click(function () {
$('#gvPerformanceResult')
.find('> tbody > tr > td:first-child > input[type="checkbox"]')
.prop('checked', true);
});
But I can't really think of a scenario that could warrant something like this. If you have to resort to this, you might be having some deeper problems in your HTML structure.
Upvotes: 5
Reputation: 24506
You do not need to loop through like that, just tell jQuery to find the checkboxes inside the table and mark them as checked (assuming the only checkboxes in the table are the ones you want included in the 'select all').
$('#HypSelectAll').click(function () {
$("#gvPerformanceResult").find("input[type=checkbox]").prop("checked", true);
});
Upvotes: 1