最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to loop over elements in form using getElementsByName? - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 14

Take 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";
}
发布评论

评论列表(0)

  1. 暂无评论