I want to make a page that has a table of various webpages with checkboxes next to each. I want the user to be able to select multiple sites then search the sites using a google bar. I have a table where each cell has a form filled with checkboxes. each cell has a checkall button that checks all the options in that cell. I would like to add a checkbox to select all the options on the page. (yes I could just leave this option out but I kind of want to know how to access all the boxes in the cells anyway so that I can search with google like I want.) here is basically what I have. Its the section inside checkPage function that needs help at this point
<html>
<head>
<script type="text/javascript">
function checkAll(checkname, bx) {
for (i = 0; i < checkname.length; i++){
checkname[i].checked = bx.checked? true:false;
}
}
function checkPage(bx){
var bxs = document.getElementByTagName ( "table" ).getElementsByTagName ( "link" );
for(i = 0; i < bxs.length; i++){
bxs[i].checked = bx.checked? true:false;
}
}
</script>
</head>
<body>
<input type="checkbox" name="pageCheck" value="yes" onClick="checkPage(this)"><b>Check Page</b>
<table border="1" name ="table">
<tr>
<td name ="list00">
<form name ="list00">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list00.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form>
</td>
<td><form name ="list01">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list01.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form></td>
</tr>
<tr>
<td><form name ="list10">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list10.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form></td>
<td><form name ="list11">
<input type="checkbox" name="Check_ctr" value="yes" onClick="checkAll(document.list11.link, this)"><b>Check All</b><dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
<input type="checkbox" name="link" value="something.com">something.com<dd>
</form></td>
</tr>
</table>
</body>
</html>
In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.
The “select all” checkbox is a User Interface element present in some Web applications that allows you to select multiple items or data fields at once so you can perform the same action on the selected items.
function checkAll(bx) {
var cbs = document.getElementsByTagName('input');
for(var i=0; i < cbs.length; i++) {
if(cbs[i].type == 'checkbox') {
cbs[i].checked = bx.checked;
}
}
}
Have that function be called from the onclick attribute of your checkbox to check all
<input type="checkbox" onclick="checkAll(this)">
Edit I misread your question a little, i see you have attempted it in your code. the getElementsByTagName has to be plural which you may have typo'd there and has to be a tag as specified by the answer above
Edit: Passing the master checkbox as a parameter would allow for toggling check/uncheck as suggested by vol7ron and has been modified in this answer appropriately.
The question asks for all checkboxes on the page so this would suffice.
However, providing control of which elements to look for checkboxes can be achieved in may ways, too many to go into detail but examples could be document.getElementById(id).getElementsByTagName if all checkboxes to be controlled are branched nodes from one element.
Otherwise, you can iterate through a further tag name retrieval / custom class name retrieval to name a few.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With