Reputation: 1284
I am trying to implement the feature of checking and unchecking all checkboxes in a grid view at once. I created a main checkbox that will be used to check all the checkboxes at once
<input id="main-checkbox" type="checkbox" name="messages-checkall" value="all" checked="checked" onclick='checkedAll();'>
The checkedAll() function is implemeted as follows
<script type='text/javaScript'>
function checkedAll () {
var inputs = document.getElementsByTagName('input');
var checkboxes = [];
if (document.getElementById('main-checkbox').checked === "checked") {check = false} else {check = true};
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == 'checkbox') {
inputs[i].checked = check;
}
}
}
</script>
but this is not working.
Note - I cannot create a form with all the checkboxes and then handle all the checkboxes in the form.
Please tell if you want other detail.
Upvotes: 0
Views: 3916
Reputation: 205
var objCheckBoxes = document.getElementById('containerId').getElementsByTagName('input');
var countCheckBoxes = objCheckBoxes.length;
if(!countCheckBoxes)
{
objCheckBoxes.checked = true/false;
}
else
{
for(var i = 0; i < countCheckBoxes; i++)
{
objCheckBoxes[i].checked = true/false;
}
}
In html
<ul id="containerId">
<li><input type="checkbox" name="selected" value="Some text description 1"></li>
<li><input type="checkbox" name="selected" value="Some text description 2"></li>
</ul>
Upvotes: 0
Reputation: 47482
Change
if (document.getElementById('main-checkbox').checked === "checked") {check = false} else {check = true};
To
var check = document.getElementById('main-checkbox').checked
Upvotes: 3