Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing elements from an array: pop is not a function (javascript)

thead = new Array();
alert(thead.length);
thead = document.getElementsByTagName("th");
alert(thead.length);


thead.pop();
alert(thead.length);

document.getElementsByTagName("th") returns an array of elements, so thead variable should be an array, if so, then why it gives me the error "thead.pop() is not a function"?

like image 882
Aikanáro Avatar asked Dec 11 '25 09:12

Aikanáro


1 Answers

getElementsByTagName (docs) does not return an Array, it returns a NodeList. As said by the linked NodeList docs:

NodeList are used very much like arrays and it would be tempting to use Array.prototype methods on them. This is, however, impossible.

There are some Array like things you can do with a NodeList, and you can even .apply some Array.prototype methods to them, but you should read the docs to avoid the "gotchas", especially where problems with the NodeList being "live" could bite you.

like image 111
JAAulde Avatar answered Dec 12 '25 22:12

JAAulde