Click to See Complete Forum and Search --> : Javascript getElementById()


zoey7907
01-26-2008, 02:52 PM
I have a form with 52 checkboxes that all have the same name (Day). I gave them all the same name because javascript will put them into an array and i can loop through the values easily. Now i need to send all these values to a php file to put the information into a database. I made a hidden input field for each day and now i would like to make a function that will loop through all the values of Day and assign a value to the corresponding hidden field accordingly. I gave all the hidden fields the same id "days". I tried to use getElementById but i don't think it is putting all the elements into an array. Any suggestions?

here's the function:
function assign()
{
var x=document.getElementById('days');
var z;
alert ("x " + x );
for(z=0; z<x.length; z++)
{

if (Day[z].checked)
{x[z].value = "T";}
else
{x[z].value = "F";}
alert ( x[z].vlaue );
}
}


Thanks!

Weedpacket
01-26-2008, 06:17 PM
I gave all the hidden fields the same id "days". I tried to use getElementById but i don't think it is putting all the elements into an array. Any suggestions?For a start, don't use the same id value for more than one element in a document.
http://www.w3.org/TR/REC-html40/struct/global.html#adef-id

That's why it's called "getElementById" not "getElementsById".
Try using "getElementsByName" (note the 's').

rulian
01-28-2008, 05:52 PM
their names also have to be arrayed like "name[]"
and you can translate through the document.form method to access them directly. that way you can have the properties of form elements to do any sort of validation.
document.form.checkboxname.length will be the number of your checkbox's