How to iterate over elements in a form using getElementsByName?

I would like to select certain elements in the form by their name, so I assume the use of getElementsByName (name). Then I would like to add value to these elements. How to do it?

boxesEL = document.getElementsByName(boxesName);

for(var x=0;x<=boxesEL.length;x++){
    boxesEL[x].value = "some value";
}

I get the error boxesEL [x] - undefined.

+3
source share
1 answer

Print the "=" sign when comparing in a for loop. You loop too many times. Length gives you the number of elements - the maximum index of the collection will be less because it is based on zero.

for(var x=0; x < boxesEL.length; x++)   // comparison should be "<" not "<="
{
    boxesEL[x].value = "some value";
}
+11
source

Source: https://habr.com/ru/post/1718847/


All Articles