Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML getElementsByClassName returns HTMLCollection with length of 0

I am trying to use the js document.getElementsByClassName to locate an html element, which is actually the header of a table.

For the following codes:

console.log(document.getElementsByClassName('gtableheader'));

From the Firebug, I can see it log a HTMLCollection, and when I click it, it shows:

-> 0         tr.gtableheader
   length    1

So it do locate the element I want.

But when I using:

console.log(document.getElementsByClassName('gtableheader').length);

Then output is 0. That's so weird, any ideas about this?

like image 847
chrisTina Avatar asked Nov 12 '15 20:11

chrisTina


People also ask

What does getElementsByClassName () function return?

getElementsByClassName() The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.

How do I find the length of a HTMLCollection?

The length property returns the number of elements in an HTMLCollection. The length property is read-only. The length property is useful when you want to loop through an HTMLCollection.

Why is getElementsByClassName not working?

There are several issues: Class names (and IDs) are not allowed to start with a digit. You have to pass a class to getElementsByClassName() . You have to iterate of the result set.

What does getElementsByClassName return node?

The getElementsByClassName() method returns a collection of elements with a specified class name(s). The getElementsByClassName() method returns an HTMLCollection.


2 Answers

That's because the getElementsByClassName returns a live collection. the length property of the object is 0 because at that point of time there is no element with that className in the DOM. Since the console shows the live representation of an object, it shows all the matching elements when the elements are added to the DOM.

DOM parser parses the documents from top to bottom, when it reaches to a tag, it parses it and adds the DOM representation of it (an instance of HTMLElement interface) to the Document Object Model. You should either move the script tag to the end of body tag or listen to DOMContentLoaded event which is fired when the initial HTML document has been completely loaded and parsed.

like image 118
undefined Avatar answered Sep 21 '22 07:09

undefined


Using getElementsByClassName() will return all the elements with that class name in a document as a NodeList. This object represents a collection of nodes that can be accessed by index numbers, which starts in 0. In order to access the elements in the NodeList you will have to use a loop.

When you console.log(document.getElementsByClassName('gtableheader').length); you see 0 because when you run it there is no element with class gtableheader. You are able to see the items in the console because document.getElementsByClassName() returns a live collection that is updated when the new elements are added.

As well, in the code you are using and the length is 0, you can use the code below to access the class name.

document.getElementsByClassName('gtableheader')[0].style.color="red";

If you want to access all the elements in the class you can use a for loop.

var x = document.getElementsByClassName('gtableheader');
for (var i = 0; i < x.length; i++) {
    x[i].style.color = "red";
}

More information: http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

like image 22
Victor Luna Avatar answered Sep 22 '22 07:09

Victor Luna