Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  11] [ 4]  / answers: 1 / hits: 16464  / 15 Years ago, tue, september 29, 2009, 12:00:00

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.


More From » loops

 Answers
10

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;
}

[#98593] Friday, September 25, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keyonnaelled

Total Points: 35
Total Questions: 113
Total Answers: 99

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
;