I have the following HTML structure using bootstrap. I need to select a checkbox to select all the checkboxes but the javascript codes I've been testing have not worked (I'm not very good at javascript). I think these codes do not work because they do not take into account the "active" class that uses bootstrap.
The following javascript not worked for me:
<script>
function toggle(source) {
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i] != source)
checkboxes[i].checked = source.checked;
}
}
</script>
Test
<form action="" method="post" class="form-horizontal">
<fieldset>
<legend>EXAMPLE</legend>
<div class="form-group rowauto" data-toggle="buttons">
<label for="selectall">SELECT/UNSELECT ALL:</label>
<label class="btn btn-success active checkboxkox">
<input type="checkbox" id="selectall" name="selectall" autocomplete="off" checked>
<span class="glyphicon glyphicon-ok"></span>
</label>
</div>
</fieldset>
<fieldset>
<div class="row rowauto">
<div class="col-sm-1">
<div class="form-group rowauto" data-toggle="buttons">
<label class="btn btn-success active checkboxkox">
<input type="checkbox" id="check_1" name="check_1" autocomplete="off" checked>
<span class="glyphicon glyphicon-ok"></span>
</label>
</div>
</div>
</div>
<div class="row rowauto">
<div class="col-sm-1">
<div class="form-group rowauto" data-toggle="buttons">
<label class="btn btn-success active checkboxkox">
<input type="checkbox" id="check_2" name="check_2" autocomplete="off" checked>
<span class="glyphicon glyphicon-ok"></span>
</label>
</div>
</div>
</div>
</fieldset>
</form>
The IDs of my checkboxs are generated by a loop "foreach" in PHP