Rahul Singh
Rahul Singh

Reputation: 1632

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);
}

Upvotes: 2

Views: 25781

Answers (5)

IsHacker
IsHacker

Reputation: 25

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().

Upvotes: 0

Kiran
Kiran

Reputation: 20303

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); 
} 

Upvotes: 2

KodeFor.Me
KodeFor.Me

Reputation: 13521

The function is not getElementsById but getElementById.

There is no plural form on Element

Upvotes: 4

James Allardice
James Allardice

Reputation: 166031

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.

Upvotes: 8

Dave Newton
Dave Newton

Reputation: 160281

The name of the function is getElementById.

Upvotes: 1

Related Questions