The Wayback Machine - https://web.archive.org/web/20231217054729/https://www.geeksforgeeks.org/html-dom-queryselectorall-method/
Open In App
Related Articles

HTML DOM querySelectorAll() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The querySelectorAll() method in HTML is used to return a collection of an element’s child elements that match a specified CSS selector(s), as a static NodeList object. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0. 

Note: If we want to apply CSS property to all the child nodes that match the specified selector, then we can just iterate through all nodes and apply that particular property.

Syntax:  

element.querySelectorAll(selectors)

Selectors are the required field. It specifies one or more CSS selectors to match the element. These selectors are used to select HTML elements based on their id, classes, types, etc. 
In the case of multiple selectors, a comma is used to separate each selector.

Example: In this example, we will use querySelectorAll() method

HTML




<!DOCTYPE html>
<html>
   
<head>
    <title>DOM querySelectorAll() Method</title>
    <style>
        #geek {
            border: 1px solid black;
            margin: 5px;
        }
    </style>
</head>
   
<body style="text-align: center;">
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>querySelectorAll() Method</h2>
    <div id="geek">
        <p>This is paragraph 1.</p>
        <p>This is paragraph 2.</p>
    </div>
    <button onclick="myFunction()">Try it</button>
   
    <script>
        function myFunction() {
            let x = document.getElementById("geek")
                    .querySelectorAll("p");
            let i;
            for (i = 0; i < x.length; i++) {
                x[i].style.backgroundColor = "green";
                x[i].style.color = "white";
            }
        }
    </script>
</body>
 
</html>


Output: 

 

Supported Browsers: The browser supported by the querySelectorAll() method are listed below: 

  • Apple Safari 3.1
  • Google Chrome 1.0
  • Edge 12.0
  • Firefox 3.5
  • Opera 10.0
  • Internet Explorer 9.0

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 16 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials