7

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.

1 Answer 1

15

Take out the "=" sign in the comparison 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++)   // comparison should be "<" not "<="
{
    boxesEL[x].value = "some value";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.