Why does JavaScript getElementsByClassName provide an object that is NOT an array?

I am trying to get a list in JavaScript (not using jQuery) of all the elements on the page with a specific class name. Therefore, I use the getElementsByClassName () function as follows:

var expand_buttons = document.getElementsByClassName('expand');
console.log(expand_buttons, expand_buttons.length, expand_buttons[0]);

Please note that I have three anchor elements on my page with a class extension. This output is console.log ()

[] 0 undefined

Next, for kicks, I threw expand_buttons into my own array as follows:

var newArray = new Array(expand_buttons);
console.log(newArray, newArray.length);

It unexpectedly displays

[NodeList[3]] 1

and I can click on the nodelist node and see the attributes of the three "expand" anchor elements on the page. It's also worth noting that I managed to get my code to work on the w3schools page .

, document.getElementsByName ( ) , , 0. , array_name[0] , "undefined", , , .

- , ? DOM, jQuery , JavaScript.

,

ParagonRG

+5
2

JavaScript, -. API ( document), DOM NodeList. NodeList , , ( ).

NodeList :

var nodeArr = Array.prototype.slice.call(theNodeList, 0);

ES2015:

var nodeArr = Array.from(theNodeList);

JavaScript , API-, JavaScript. -. DOM , JavaScript; , .

, " ".

+8

, "live" , NodeList:

NodeList . , DOM .

+3

All Articles