03. Selecting multiple elements NodeLists

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.

NodeLists

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.

Live vs. 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.

3 ways to access NodeLists

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.

1) Getting elements by class

Using the 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 getElementsByClassName().

var allItems = getElementsByClassName('item')

2) Selecting elements by tag

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 <li>:

var allListItems = getElementsByTagName('li')

3) Getting elements matching a CSS selector

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).

querySelectorAll('a[href*="http://"]')

Traversing the DOM - multiple elements

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 childNode property.

var list = querySelector('ol')
var orderedListItems = list.childNodes

Here, orderedListItems will always be a NodeList.

Handling 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.

Accessing each node

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:

myNodeList[0]
myNodeList.item(0)

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.

Looping through

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)

Shortcuts to grabbing multiple elements

anchors
All <a> elements with a name attribute.
forms
All the form elements within a document.
images
All <img> elements
links
All link elements with an href attribute.

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Build modern and responsive webpages

High Performance Web Sites

Build modern and responsive webpages Try Front-end

Want your web site to display more quickly? This book presents 14 specific rules that will cut 25% to 50% off response time when users request a page. Author Steve Souders, in his job as Chief Performance Yahoo!, collected these best practices while optimizing some of the most-visited pages on the Web.

$ Check price
29.9929.99Amazon 4.5 logo(63+ reviews)

More Front-end resources

Ad