HTML getElementsByClassName returns HTMLCollection with length of 0

chrisTina

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?

Victor Luna

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

this.getClass().getFields().length; always returns 0

Java.io.File.length() returns 0

The .length function always returns 0

Why the length of the slice in http.Header returns 0?

object HTMLcollection[0] keeps returning undefined

Javascript HTML collection showing as 0 length

Length of getElementsByClassName() array changes when removing class

$(':target') returns length 0 in Internet Explorer 11

targetSdkVersion 23 returns 0 length array via accountManager.getAccounts()

Unable to access elements in HTMLCollection from getElementsByClassName

Flutter (Dart) Looping through JSON results - .length returns 0

Array return 0 length, unable to call it in html

can someone explain me why alert.length returns "0" and why Array.length or Object.length returns 1

Array Length returns 0

HTML5 video - video.buffered.length returns 0 on IE 10/11

document.getElementsByClassName('name') has a length of 0

UITextField, returns length of 0 even when entered some text

Java - file.length() returns 0

element.length returns 0 even if the element exist

getElementsByClassName returns duplicate elements

PHP PDO Data_length always returns "0"

Java reflection getTypeParameters().length returns 0

GetElementsByClassName('..')[0].value = '' does not work

VueJS array returns length = 0

Can't get HTMLCollection length

My array is not empty but array.length returns 0

for loop returns 0 if array length more than 1

Is getElementById or getElementsByClassName[0] faster?

skimage hog returns no feature vector with length 0