Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document order for DOM selection method

Tags:

javascript

I know that live NodeList returned by getElementsByTagName preserves the document order of elements.

Is it true for getElementsdByClassName, getElementsByName and querySelectorAll methods?

does these method also preserve the document order?

Any DOM/HTML5 W3C standard link would be appreciated.

like image 696
P K Avatar asked Mar 02 '26 02:03

P K


1 Answers

Yes. All of them are in document order / tree order.

  • getElementsByName (DOM Level-2-HTML) returns a NodeList
  • querySelectorAll (Selectors API) returns a NodeList "in document order"
  • getElementsByTagName (DOM) returns a HTMLCollection
  • getElementsByClassName (DOM) returns a HTMLCollection

HTMLCollections and NodeLists are both specified to have

the elements are sorted in tree order.

when those are accessed via indizes. It does not really matter whether the NodeList is live or not (though of course the actual document order could change in contrast to the one preserved in the static NodeList).

like image 111
Bergi Avatar answered Mar 04 '26 14:03

Bergi