Zsolt Szatmari
Zsolt Szatmari

Reputation: 1239

HTMLCollection confusion

If I call document.getElementsByClassName('cl'), I get a HTMLCollection. It seems to contain Element objects instead of HTMLElement objects, see Flow source, or Mozilla doc citing that it contains elements which are not HTMLElements. The W3C specification writes that HTMLCollection is a historical artifact, although I cannot seem to find any suggested alternative (what is 'Elements'?).

So should I check if returned object is indeed a HTMLElement? What else could it be and when? Or is there a better way to do what getElementsByClassName and similar methods do? (without using external libraries)

Thanks in advance!

Upvotes: 0

Views: 211

Answers (1)

Madara's Ghost
Madara's Ghost

Reputation: 174937

DOM is not HTML exclusive. XML and SVG (which is a form of XML) are also supported by the DOM specifications. Element is a general object that maps to a single element of any type.

Also, the alternative is a NodeList which is returned by querySelectorAll().

Upvotes: 1

Related Questions