Selecting on element at a time is great for when you want to add a simple interactive feature, but many times you'll want to manipulate multiple elements. There are a few methods that we may use to select multiple elements, which we will see here.
The objects that act as a container to hold several node references are known as NodeLists. There are two types of NodeLists: live and static.
One thing we should mention before we begin is whether the NodeList is live or static. In a live NodeList, elements within the NodeList are updated, as scripts are run. A NodeList that is static does not change even after elements are updated on the DOM.
Note that if a method can return a NodeList, it always will. Even if it returns just one item, it is to be treated as a NodeList of size one. Let's now look at the three ways we can obtain a nodelist.
querySelector() function, we were only able to select the very first element it approached. To access all the elements of a certain class, we use
var allItems = getElementsByClassName('item')
Another way to perform a similar function as above is to select an element by its HTML tag. For example, if you were to select all list elements
var allListItems = getElementsByTagName('li')
To obtain even more power and flexibility, we can use the
querySelectorAll() function, which matches elements that match a CSS selector query. This method returns a static NodeList. The following selects all links that are external (or exit the domain).
Another way we may traverse the DOM list is by getting all child nodes of a certain element. We may access an element's child nodes through its
var list = querySelector('ol') var orderedListItems = list.childNodes
orderedListItems will always be a NodeList.
Let's now learn how to handle a NodeList. Recall that NodeLists are a type of collection - a special object that has array-like syntax but object-like capabilities.
There are two ways we can access a node within a NodeList - the array-like and the object-like way. Since NodeLists use a zero-based index, we are choosing the first item in this example:
Try accessing through the array method, as this is easier to type out, and faster to process.
You can also use the firstItem property, which has the same function as above.
Oftentimes you'll want to loop through all the nodes and apply some property to everything. Since we can access each node with its index and know the NodeList's length, we may access and apply a function to all elements.
for (var i = 0; i < myNodeList.length; i++) // Apply things here. myNodeList.item(i)
<a>elements with a name attribute.
Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.$$ Check price
This book teaches HTML as if it's for anyone - hobbyists, students, and professionals - and it's full-color throughout. It utilizes information graphics and lifestyle photography to explain the topics in a simple way that is engaging. You can progress through the chapters from beginning to end or just dip into topics of particular interest at your leisure.$ Check price