YouTip LogoYouTip

Js Htmldom Nodelist

# JavaScript HTML DOM NodeList The **NodeList** object is a collection of nodes (a node list) obtained from the document. The NodeList object is similar to the (#) object. Some older browser methods (like **getElementsByClassName()**) return a NodeList object, not an HTMLCollection object. All browsers' **childNodes** property returns a NodeList object. Most browsers' **querySelectorAll()** returns a NodeList object. The following code selects all

nodes in the document: ## Example var myNodeList = document.querySelectorAll("p"); Elements in a NodeList can be accessed by index (starting at 0). Accessing the second

element can be done with the following code: y = myNodeList; [Try it Β»](#) * * * ## NodeList Object length Property The NodeList object length property defines the number of elements in the node list. ## Example var myNodelist = document.querySelectorAll("p"); document.getElementById("demo").innerHTML = myNodelist.length; [Try it Β»](#) ### Example Explained Get the collection of

elements: var myNodelist = document.querySelectorAll("p"); Display the number of elements in the node list: document.getElementById("demo").innerHTML = myNodelist.length; The length property is often used to loop through a node list. ## Example Change the background color of all

elements in the node list: var myNodelist = document.querySelectorAll("p"); var i; for(i = 0; i **A NodeList is not an array!** > > > A NodeList might look like an array, but it is not. > > > You can use an index to access elements, just like an array. > > > A NodeList cannot use array methods: valueOf(), pop(), push(), or join(). >

← Tortoisesvn IntroMet Document Queryselectorall β†’