12-27-2008, 06:42 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
How to locate certain checkboxes
I have a form that dynamically creates checkboxes. I want to put a checkbox above those so that the user can click on it to check/uncheck all of them at once. But I can't figure out how to find the checkboxes. Here's what I am trying to do:
This creates the single checkbox that I want to click to toggle all the others.
PHP Code:
<input type="checkbox" name="checkall" onclick="ChangeCheckStatus();">
Later in the form is the code to create the individual checkboxes.
PHP Code:
for ($i = 0; $i < $some_variable; ++$i)
{
<input type="checkbox" name="checkall_"<?php echo $i; ?>">
}
I know I will need to do a loop to see them all but I used the following just to see if the basic code is working. But when I click on the main checkbox, nothing is displayed. If I change the alert to alert(elm.length);, it displays the total number of input form elements, as expected. Would someone please point out my mistake or let me know if there is a better way to do this?
HTML Code:
<script type="text/javascript">
function ChangeClickedStatus()
{
var elm = document.getElementsByTagName("input");
alert(elm[0].name);
}
</script>
|
|
|
|