Why is getElementsByTagName returning undefined?
dom, firefox, html, javascript
Solution
edit:
This is a bug in firebug and is fixed by upgrading to 1.10.0a7
Because it is impossible for this method to return `undefined`, there are 2 possibilities:
- Your debugging tools are lying to you
- `document.getElementsByTagName` is not referencing the original host object. It should print `function getElementsByTagName() {[native code]}` when referenced in console.
You should be able to reliably to see if it's in fact `undefined` (in firefox) with this:
delete window.alert;
window.alert(buttons);
The `delete` is a NOOP if `window.alert` is already referencing the original host object, otherwise it will restore it.
If it alerts `undefined`, you should be able to do
delete document.getElementsByTagName
to restore the host object reference.
All console references here refer to the built in Web Console that comes with firefox by default.
Problem
I'm trying to call `document.getElementsByTagName`, and I'm getting back `undefined` as a result, no matter what parameter I pass. (Even if I pass "*".) I tried Googling for it, but all the search results were about elements of the getElementsByTagName result array being undefined. What I'm getting is `undefined` as the result itself, and it's driving me up the wall. Does anyone know what can cause this? (Using Firefox 12.0. In Chrome I get the expected results.) EDIT: OK, here's sample code: ``` function buttonClick(){ var xhr = new XMLHttpRequest(); var msg = document.getElementById('message'); var buttons = document.getElementsByTagName("button"); var button, i; for (i = 0; i < buttons.length; ++i){ button = buttons[i]; msg.removeChild(button); } xhr.onreadystatechange = function() { if(xhr.readyState == 4){ handleResult(xhr.responseText, msg); } }; xhr.open("POST", location.href, true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send("cmd=MyCommand"); } ``` And the `getElementsByTagName` always returns `undefined`, whether I trace it in Firebug's Script tab or call it from the Console tab. (Also in Firebug, since this seems to be confusing people. Apparently there are way too many consoles floating around.). As proof, here's what I've been getting when I tried to use the Firebug console: ``` >>> document.getElementsByTagName("button"); undefined >>> msg.getElementsByTagName("button"); undefined >>> msg.getElementsByTagName getElementsByTagName() >>> msg.getElementsByTagName("BUTTON"); undefined >>> msg.getElementsByTagName("*"); undefined >>> document.getElementsByTagName("*"); undefined >>> document.getElementsByTagName("body"); undefined ``` The markup is (or ought to be) irrelevant. It's a valid, well-formed HTML page with some buttons and other elements on it. This JS function is attached to the `onclick` of one of the buttons. But it looks something like this: ``` <html xmlns="http://www.w3.org/1999/xhtml"><head> blah </head> <body> <script type="text/javascript" src="/myJS.js"></script> <div id="page-container"> <div id="message"><button onclick="buttonClick();">Button 1</button><button onclick="ButtonClick2()">Button 2</button></div> </div> </body></html> ```