Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is getElementsByTagName returning undefined?

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>
like image 419
Mason Wheeler Avatar asked May 27 '12 16:05

Mason Wheeler


People also ask

What does getElementsByTagName return if not found?

Return value If no elements are found, the HTMLCollection is empty.

What is getElementsByTagName return?

getElementsByTagName() The getElementsByTagName method of Document interface returns an HTMLCollection of elements with the given tag name. The complete document is searched, including the root node.

Does getElementsByTagName return an array?

getElementsByTagName - the method name itself implies that it will return multiple elements - i.e. an array. The method always returns an array, with the length equal to the number of matching elements. As such you must always access the elements by the index of the element in the array.

What is the use of getElementsByTagName in Javascript?

The getElementsByTagName() method returns a collection of all elements with a specified tag name. The getElementsByTagName() method returns an HTMLCollection. The getElementsByTagName() property is read-only.


1 Answers

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.

like image 112
Esailija Avatar answered Sep 23 '22 16:09

Esailija