I would like to select certain elements in form by their name, so I suppose using getElementsByName(name). Then I would like to add a value to these elements. How do I do this loop?
boxesEL = document.getElementsByName(boxesName);
for(var x=0;x<=boxesEL.length;x++){
boxesEL[x].value = "some value";
}
I'm getting an error boxesEL[x] is undefined.
I would like to select certain elements in form by their name, so I suppose using getElementsByName(name). Then I would like to add a value to these elements. How do I do this loop?
boxesEL = document.getElementsByName(boxesName);
for(var x=0;x<=boxesEL.length;x++){
boxesEL[x].value = "some value";
}
I'm getting an error boxesEL[x] is undefined.
Share Improve this question asked Sep 29, 2009 at 21:14 AdrianaAdriana 8,61413 gold badges38 silver badges38 bronze badges1 Answer
Reset to default 14Take out the "=" sign in the parison in the for loop. You're looping one too many times. Length gives you the number of elements - the maximum index of the collection will be one less, because it's zero based.
for(var x=0; x < boxesEL.length; x++) // parison should be "<" not "<="
{
boxesEL[x].value = "some value";
}