document.querySelectorAll() is a powerful method in JavaScript, part of the Document Object Model (DOM) API, used to select multiple elements in an HTML document. This method returns a static NodeList representing a list of the document's elements that match the specified group of selectors.

Whether you're a seasoned web developer or just starting, understanding and utilizing document.querySelectorAll() is essential for manipulating and styling multiple HTML elements efficiently. This guide will delve into this method's syntax, usage, and provide practical examples.

Syntax and Basic Usage
The basic syntax of document.querySelectorAll() is as follows:

let elements = document.querySelectorAll(selector);
The selector is a string that selects the elements you want. It can be an id, class, tag name, attribute, or a combination of these.
For instance, if you want to select all paragraph elements, you can do:

let paragraphs = document.querySelectorAll('p');
Return Type
document.querySelectorAll() returns a static NodeList. Unlike arrays, NodeLists are live, meaning they always reflect the current set of elements in the DOM, regardless of how the DOM changes.
If you need to iterate over the resulting NodeList, you might want to convert it to an Array using:

const arrayOfParagraphs = Array.from(paragraphs);
Selector Types
document.querySelectorAll() supports all CSS selectors, making it a versatile tool for selecting elements. Here are some examples:
Element Tags

Select all paragraph elements:
document.querySelectorAll('p')
Class and ID Selectors




















Select all elements with the class 'example' or the ID 'exampleId':
document.querySelectorAll('.example')
document.querySelectorAll('#exampleId')
Attribute Selectors
Select all elements with an 'href' attribute:
document.querySelectorAll('[href]')
Or with a specific 'href' value:
document.querySelectorAll('a[href="https://example.com"]')
Example: Manipulating Selected Elements
Let's say you want to change the color of all paragraph texts to red. Here's how you can do it:
First, select all paragraph elements:
let paragraphs = document.querySelectorAll('p');
Then, loop through the NodeList and change the color of each paragraph:
Remember, since NodeLists are live, you can directly modify the selected elements.
paragraphs.forEach(function(paragraph) {
paragraph.style.color = 'red';
});
Practical Use Case: Toggling Hidden Elements
A common use case of document.querySelectorAll() is to toggle hidden elements. Here's an example:
Assuming you have a button and a div:
<button onclick="toggleVisibility()">Toggle visibility</button> <div id="myDiv" style="display:none;">Hey, click the button to see me!</div>
You can write the toggleVisibility() function as:
Note that we're using CSS to hide the div.
function toggleVisibility() {
let div = document.querySelector('#myDiv');
div.style.display = (div.style.display === 'none') ? 'block' : 'none';
}
The final aspect to consider is performance. Be mindful of overusing document.querySelectorAll(), especially in certain scenarios. When choosing elements by class or attribute, using getElementsByClassName() or getElementsByAttribute() may be more efficient in some cases.
Nevertheless, document.querySelectorAll() is a powerful tool that simplifies multi-element selection. Understanding and mastering its usage will significantly enhance your web development skills. Keep practicing, keep exploring, and happy coding!