I have table that have checkbox and button out side the table that will toggle from select to unselect , how do I click anywhere on the table row and my checkbox tick regarding where I will be selected one by one and when it reaches the last table row my button change from select to unselect, I did manage to click the table row and checkbox tick , what I’m failing is when I reach the last table row and button change from select to unselect.
I have tried to get the length of row
$('tr').click(function(event) {
var $target = $(event.target);
if (!$target.is('input:checkbox')) {
var select_chk = document.getElementById("button1");
$(this).find('input:checkbox').each(function() {
if ((this.checked)) {
this.checked = false;
button.value = "Unselect"
} else {
this.checked = true;
button.value = "Select"
}
})
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" id="check" value="Select" />
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Surname</th>
<th>Gender</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td>
<td>Cliff</td>
<td>Deon</td>
<td>Male</td>
<td>52</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>Dennis</td>
<td>Van Zyl</td>
<td>Male</td>
<td>25</td>
</tr>
</tbody>
</table>
2
Answers
There were several issues.
Here is a working version that also handles clicking the button
I count the number of checked checkboxes are equal to total number of checkboxes.
I added an ID to the tbody
Also I use ternaries