I have button on my web page, and when this button is clicked I want to select all of the green check boxes in each row of my table.
I am unsure of the logic for this and would appreciate some help
This is my table:
$.each(JSON.parse(result), function (i, item) {
var row = i + 1;
$("#mainData").append(
"<tr>" +
"<td id='process_" + row + "'" + ">" + item.Process + "</td>" +
"<td id='checks_" + row + "'" + ">" + item.Checks + "</td>" +
"<td>" +
"<div class='btn-group' data-toggle='buttons'" + ">" +
"<label class='btn btn-success'" + ">" +
"<input type='checkbox' name='colours' id='green_" + row + "'" + ">" +
"<span class='glyphicon glyphicon-ok'" + "></span>" +
"</label>" +
"<label class='btn btn-warning'" + ">" +
"<input type='checkbox' name='colours' id='yellow_" + row + "'" + ">" +
"<span class='glyphicon glyphicon-ok'" + "></span>" +
"</label>" +
"<label class='btn btn-danger'" + ">" +
"<input type='checkbox' name='colours' id='red_" + row + "'" + ">" +
"<span class='glyphicon glyphicon-ok'" + "></span>" +
"</label>" +
"<label class='btn btn-default'" + ">" +
"<input type='checkbox' name='colours' id='grey_" + row + "'" + ">" +
"<span class='glyphicon glyphicon-ok'" + "></span>" +
"</label>" +
"</td>" +
"<td><textarea id=" + "'" + "comments_" + row + "'" + "type='text' placeholder='' class='form-control input-md'/></td>" +
"</tr>");
});
this is my select all button
$('#SelectAll').click(function () {
var rowCount = $('#mainData >tr').length;
var i;
for (i = 1; i <= rowCount; i++) {
$("input:checkbox").prop('checked', $(this).prop("checked"));
}
});
<input type="checkbox" class="green" />(the same for the other ids) ->$(".class").prop(...)$("input[name='colours']").prop('checked', $(this).prop("checked"));No need for a loop$("input:checkbox")selects all checkboxes on the page simultaneously, and then applies the "checked" property to all of them at once. As others have said, because the selector can make the statement apply to multiple elements at once, you don't really need the loop. Just make it more targeted by giving all the checkboxes a class according to their colour and then using e.g.$(".green")to select them.