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

Javascript error - document.getElementsById is not a function - Stack Overflow

programmeradmin1浏览0评论

Since I am calling this code in loop. But following code is giving me error as document.getElementsById is not a function. What should I do how can I call doc.getbyid in loop.

for (var z=1; z < i; z++){
       var textbox = document.getElementsById("a"+z).value;
       var textbox2 = document.getElementsById("b").value;
       var textbox3 = document.getElementsById("c").value;
       alert(textbox);
       alert(textbox2);
       alert(textbox3);
}

Since I am calling this code in loop. But following code is giving me error as document.getElementsById is not a function. What should I do how can I call doc.getbyid in loop.

for (var z=1; z < i; z++){
       var textbox = document.getElementsById("a"+z).value;
       var textbox2 = document.getElementsById("b").value;
       var textbox3 = document.getElementsById("c").value;
       alert(textbox);
       alert(textbox2);
       alert(textbox3);
}
Share Improve this question edited Oct 25, 2011 at 6:56 nnnnnn 150k30 gold badges208 silver badges247 bronze badges asked Oct 25, 2011 at 6:52 Rahul SinghRahul Singh 1,6326 gold badges23 silver badges39 bronze badges 1
  • What is your requirement ? What is the name of textbox you are looking for using document.getElementsById ??? – Gourav khanna Commented Oct 25, 2011 at 6:55
Add a comment  | 

5 Answers 5

Reset to default 8

That's because it getElementById (note the lack of the "s" on "Element"). Which makes sense if you think about it, because id values have to be unique in a document, so there will be only one "element" that matches, rather than multiple "elements".

However, there are methods that return multiple elements which do use the plural "elements", such as getElementsByTagName, so you may just be mixing them up.

The function is not getElementsById but getElementById.

There is no plural form on Element

Actually you need to use as follows:

for (var z = 1; z < i; z++) { 
    var textbox = document.getElementById("a"+z).value; 
    var textbox2 = document.getElementById("b").value; 
    var textbox3 = document.getElementById("c").value; 
    alert(textbox); 
    alert(textbox2); 
    alert(textbox3); 
} 

The name of the function is getElementById.

document.getElementsById() is not the function but document.getElementById() is. If you want to get all of the tag names, you can use document.getElementsByTagName() and if you want to get specific class elements you can use document.getElementsByClassName().

发布评论

评论列表(0)

  1. 暂无评论