0

what is the nature of the result of getElementsByClassName() ?

It was mentioned that getElementsByClassName() returns an array of the elements of with the class between the parentheses but i think it's a node list not an array. the difference that a node list does not have array methods like (push,slice ...) but both have a length property.

27th Sep 2018, 3:52 PM
wajdi hamrouni
wajdi hamrouni - avatar
1 Antwort
+ 1
var el = document.getElementsByClassName("test") alert(typeof el) // object...just like [] and new Array() will report, sigh alert(el) // [object HTMLCollection] // dump iterable properties/functions, etc (can fail on security objects; use try...catch) for(var i in el) document.write("<b>" + i + ": </b>" + el[i] + "<br/>"); Note: writing to the document will change the captured DOM; if you do this you may see a length of 0. To avoid this you could set an element's innerHTML instead of using document.write(). Reference: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection
27th Sep 2018, 4:10 PM
Kirk Schafer
Kirk Schafer - avatar