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
5 Answers
Reset to default 8That'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()
.