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"?
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"?
-
Are you trying to remove from the DOM, or just from the list? If you actually want it removed from the DOM, do this...
var last = thead[thead.length - 1]; last.parentNode.removeChild(last);
The element will be removed from the DOM, as well as thethead
list. – user1106925 Commented Jul 5, 2012 at 16:24
3 Answers
Reset to default 7getElementsByTagName
(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.
getElementsByTagName()
returns a DOM 2 NodeList
, not an Array
.
Technically, document.getElementsByTagName
returns a NodeList
object, which has no pop
function.
Try alert(Array.isArray(thead))
, and you'll see that it returns false