Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript error - document.getElementsById is not a function

Tags:

javascript

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);
}
like image 530
Rahul Singh Avatar asked Apr 30 '26 09:04

Rahul Singh


2 Answers

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.

like image 176
James Allardice Avatar answered May 02 '26 21:05

James Allardice


The function is not getElementsById but getElementById.

There is no plural form on Element

like image 42
KodeFor.Me Avatar answered May 02 '26 23:05

KodeFor.Me



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!