What I have tried is to make a checkbox when clicked it automatically checks the other checkbox from it’s row but when I tried to check the other rows, it does not work. I’ve tried to make a loop but instead of checking each, it checks all. I hope someone can help me. Thank you so much in advance!
Here is my whole code, I am using dataTables
JavaScript
x
8
1
$(document).ready(function() {
2
$('#is_checked_status').on('click', function() {
3
var checkAll = this.checked;
4
$('input[id=subCheckbox]').each(function() {
5
this.checked = checkAll;
6
});
7
});
8
});
JavaScript
1
76
76
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div class="row">
3
<div class="col-lg-12">
4
<div class="panel panel-default">
5
<div class="panel-heading">
6
Emailed Data for approval
7
</div>
8
<!-- /.panel-heading -->
9
<div class="panel-body">
10
<div class="table-responsive">
11
12
13
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables">
14
<thead>
15
<tr>
16
<th> Control Number </th>
17
<th> Tools Specification </th>
18
<th> Supplier </th>
19
<th>
20
<center> Status </center>
21
</th>
22
<th>
23
<center> Select data to approve </center>
24
</th>
25
<th>
26
<center> ID </center>
27
</th>
28
</tr>
29
</thead>
30
<?php
31
$con->next_result();
32
$result = mysqli_query($con,"CALL GetAllRequestedTools()"); ?>
33
<tbody>
34
<?php
35
while ($row = mysqli_fetch_assoc($result))
36
{
37
echo "<tr>";
38
echo "<td><a href='edit_tools_approval.php?
39
ID=".$row['ID']."' style='color:red;'>" .
40
$row['reg_input'] . "</a></td>";
41
echo "<td>" . $row['reg_tools_spec'] . "</td>";
42
echo "<td>" . $row['reg_supplier'] . "</td>";
43
?>
44
45
<td>
46
<center>
47
<label data-id="<?php echo $row['ID'];?>" class="statusButton <?php echo ($row['req_stats'])? 'label-success': 'label-danger'?>">
48
<?php echo ($row['req_stats'])? 'Approved' : 'Pending'?>
49
</label>
50
</center>
51
</td>
52
53
<td>
54
<center>
55
<input name="chk_status[]" id="is_checked_status" type="checkbox" value="<?php echo $row['req_stats'];?>">
56
</center>
57
</td>
58
59
<td>
60
<center>
61
<input name="chk_id[]" type="checkbox" id="subCheckbox" value="<?php echo $row['ID'];?>">
62
</center>
63
</td>
64
65
<?php
66
echo "</tr>";
67
}
68
?>
69
</tbody>
70
</table>
71
72
</div>
73
</div>
74
</div>
75
</div>
76
</div>
Advertisement
Answer
Ids are singular so you can not use an id multiple times. Change all of the ids to a class.
After you remove the ids and swap it to a class. You will need find the row the checkbox is in. And then select all the checkboxes in that row.
JavaScript
1
11
11
1
$(document).ready(function() {
2
$('.is_checked_status').on('click', function() {
3
var checkAll = this.checked;
4
// find the row
5
var row = $(this).closest("tr");
6
// find the checkboxes in the row
7
row.find('input.subCheckbox').each(function() {
8
this.checked = checkAll;
9
});
10
});
11
});
JavaScript
1
17
17
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<table>
3
<tr>
4
<td><input type="checkbox" class="is_checked_status" /></td>
5
<td><input type="checkbox" class="subCheckbox" /></td>
6
<td><input type="checkbox" class="subCheckbox" /></td>
7
<td><input type="checkbox" class="subCheckbox" /></td>
8
<td><input type="checkbox" class="subCheckbox" /></td>
9
</tr>
10
<tr>
11
<td><input type="checkbox" class="is_checked_status" /></td>
12
<td><input type="checkbox" class="subCheckbox" /></td>
13
<td><input type="checkbox" class="subCheckbox" /></td>
14
<td><input type="checkbox" class="subCheckbox" /></td>
15
<td><input type="checkbox" class="subCheckbox" /></td>
16
</tr>
17
</table>