Javascript Toggle All Checkboxes

Here is a nifty little tool I modified to toggle all checkboxes on a page. I use this when displaying a list of files or items that need to be processed on form submit.


<SCRIPT LANGUAGE="JavaScript">
//Toggle all Checkboxes
function toggle_all(x){
//alert('toggle = '+ x);
var inputlist = document.getElementsByTagName("input");
for (i = 1; i < inputlist.length; i++) { if ( inputlist[i].getAttribute("type") == 'checkbox' ) { // look only at input elements that are checkboxes if (x == false) inputlist[i].checked = false else inputlist[i].checked = true; } } } </script> Put this above the list of items that have a checkbox. Notice that above I start the counter at 1 rather than zero so it ignores this checkbox. <input type="checkbox" name="togall" onclick="javascript: toggle_all(this.checked);">

Cup size   
Select size then click on coffee cup.
This entry was posted in Code, Javascript, Snippets. Bookmark the permalink.