Forum Moderators: open
Could someone please show me how to do a 'select all' javascript thing? Here's the example:
<form method="post" action="script.php">
<input type="checkbox" name="test" value="select_all" />Select All
<input type="checkbox" name="test" value="1" />Choose 1
<input type="checkbox" name="test" value="2" />Choose 2
<input type="checkbox" name="test" value="3" />Choose 3
</form>
Many thanks,
Nick
1. The checkboxes will all be available in an array (document.forms[0].test, to be precise).
2. Each individual checkbox has a checked property.
The "Select All" box needs an onlick handler:
<input type="checkbox" name="test" value="select_all" onclick="selectAll(this.form.test);" />
the function selectAll() will need to cycle through each element of the array, setting the checked property of each appropriately:
function selectAll(t){
for(i=1; i<t.length; i++) t[i].checked=t[0].checked;
}
Notice how it sets each checked property to the value of the checked property of the "Select All" box? This means that it works both ways: it selects all when you put a checkmark in, and deselects all when you remove the checkmark.
I've come up with a solution, though I don't think it would work if I had multiple checkbox groups as it would check all of them...
I just dropped the name of the checkboxes from the onclick like this:
onclick="selectAll(this.form)"
Works fine, but limited I suspect...
Nick
The name attribute should be name="test[]" -- that is, there should be nothing inside the brackets.
Remember that checkboxes with the same name are all put into an array. What you've done is given each checkbox a different name. Instead of each checkbox being an element of array test[] they are now separate checkboxes, unconnected from each other:
name="test[abc@example.com]"
name="test[xyz@example.org]"
...and so on.
Obviously, your browser is allowing you to access each checkbox as if it were an element of the Form object -- I'm not sure if it's supposed to, so it might not work on all browsers.
What this means is that if you have other single checkboxes floating around, the script -- as you have amended it -- will go through and check/uncheck all those, too, and that might not be what you want. In fact, the script is going through all the Form elements and setting the checked property of each to true (this will, luckily, only have an effect with checkboxes).
Try the following changes:
Change the name attribute on all the checkboxes to "test[]"
Change the onclick handler to "selectAll(this.form.test[]);"
If that causes problems, try changing the onclick handler to "selectAll(this.form['test[]']);"
Here's the relevant code, though I can't see much being done about it...
/* generate form */
$text=<<<EOF
<form method="post"
action="mailit.php">
<table id="studentsTable">
<caption>Select students to email to</caption>
<tr>
<th class="select">Select</th>
<th>Name</th>
</tr>
<tr>
<td class="select"><input type="checkbox" name="emails"
value="" class="checkbox"
onclick="selectAll(this.form);"/></td>
<td class="bold">Email all students</td>
</tr>
EOF;foreach(sort_students() as $key => $val) {
$text.=<<<EOF
<tr>
<td class="select"><input type="checkbox" class="checkbox"
name="emails[$key]"
value="$key" /></td>
<td>$val</td>
</tr>
EOF;
}
$text.=<<<EOF
</table>
Thanks for the help rewboss, don't feel obliged to pursue this, I'm sure you have other stuff to do ;)
Nick
All you'd have to do is change the select all checkbox to this:
<input type="checkbox" name="test[]" value="select_all" onclick="selectAll(this.form['emails\[\]']);" />
Note that the brackets need to be escaped since they actually are used for other purposes normally. If you still need the $key value in the name, it gets a whole lot more complicated.
document.forms[0] is an array as well, comtaining all the elements in the form. What you could do is something like this:
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
function selectAll(){
t=document.forms[0].length;
for(i=1; i<t; i++) document.forms[0][i].checked=document.forms[0][0].checked;
}
</script>
</head>
<body>
<form method="post" action="script.php">
<input type="checkbox" name="test[]" value="select_all" onclick="selectAll();" />
<input type="checkbox" name="test[a]" value="1" />Choose 1
<input type="checkbox" name="test[b]" value="2" />Choose 2
<input type="checkbox" name="test[c]" value="3" />Choose 3
</form>
</body>
</html>
Now, say that you have a submit button or anything else after the checkboxes .. Well, simply don't try to check the last object then.
function selectAll(){
t=document.forms[0].length;
for(i=1; i<t-1; i++) document.forms[0][i].checked=document.forms[0][0].checked;
}
function selectAll(obj) {
var checkBoxes = document.getElementsByTagName('input');
for (i = 0; i < checkBoxes.length; i++) {
if (obj.checked == true) {
checkBoxes[i].checked = true; // this checks all the boxes
} else {
checkBoxes[i].checked = false; // this unchecks all the boxes
}
}
} and call it like this:
selectAll(this);