'Javascript error - document.getElementsById is not a function
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);
}
Solution 1:[1]
The function is not getElementsById but getElementById.
There is no plural form on Element
Solution 2:[2]
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);
}
Solution 3:[3]
The name of the function is getElementById.
Solution 4:[4]
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().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KodeFor.Me |
| Solution 2 | Dave Newton |
| Solution 3 | Dave Newton |
| Solution 4 | Ishayu Roy |
