querySelector and querySelectorAll Examples

querySelector

The general syntax for this method is:

let element = document.querySelector("CSS selector");

Uses a CSS selector pattern and CSS selector rules to find a matching element. Returns the FIRST matching element. If NO match is found, null is returned.

CSS Selector Examples

  • class selector: document.querySelector(".class-name");
  • tag selector: document.querySelector("div");
  • id selector: document.querySelector("#main");

Tip

You can use any valid CSS selector with querySelector. The selectors can be simple like querySelector("div") or complex like querySelector("#main div .summary").

Example

 1<!DOCTYPE html>
 2<html>
 3   <head>
 4      <title>DOM Examples</title>
 5      <style>
 6            .main {
 7               font-weight: bold;
 8            }
 9      </style>
10   </head>
11   <body>
12      <h1>querySelector Example</h1>
13      <p id="description" class="main">
14            querySelector's power is exceeded only by it's mystery.
15      </p>
16      <div id="response">
17            It's not that mysterious, querySelector selects elements
18            using the same rules as CSS selectors.
19      </div>
20      <script>
21            // selects the <p> using class selector
22            let main = document.querySelector(".main");
23            console.log(main.innerHTML.trim());
24            main.style.color = "blue";
25
26            // Selects the <div> using tag selector
27            let response = document.querySelector("div");
28            console.log(response.innerHTML.trim());
29            response.style.color = "red";
30      </script>
31   </body>
32</html>

Console Output

querySelector's power is exceeded only by it's mystery.
It's not that mysterious, querySelector selects elements
using the same rules as CSS selectors.

querySelectorAll

The general syntax for this method is:

let elements = document.querySelectorAll("CSS selector");

Uses a CSS selector pattern and CSS selector rules to find a matching elements. Returns ALL elements that match the selector. If NO match is found, null is returned.

Example

 1<!DOCTYPE html>
 2<html>
 3   <head>
 4      <title>DOM Examples</title>
 5      <style>
 6            .red {
 7               color: red;
 8            }
 9            .purple {
10               color: purple;
11            }
12      </style>
13   </head>
14   <body>
15      <h1>querySelectorAll Example</h1>
16
17      <h2>Red Fruits</h2>
18      <ul class="red">
19         <li>Strawberry</li>
20         <li>Raspberry</li>
21         <li>Cherry</li>
22      </ul>
23
24      <h2>Purple Fruits</h2>
25      <ul class="purple">
26         <li>Blackberry</li>
27         <li>Plums</li>
28         <li>Grapes</li>
29      </ul>
30
31      <script>
32            // Selects ALL the <li> elements and adds text to each one
33            let listItems = document.querySelectorAll("li");
34            for (let i=0; i < listItems.length; i++) {
35               listItems[i].innerHTML += " is yummy"
36            }
37
38            // Selects the PURPLE <li> elements and make them bold
39            let purpleItems = document.querySelectorAll(".purple li");
40            for (let i=0; i < purpleItems.length; i++) {
41               purpleItems[i].innerHTML += "!!!"
42         }
43
44         // Console log the contents of the first items in each list
45         // Remember that querySelector returns only the FIRST match
46         let firstRed = document.querySelector(".red li");
47         console.log("contents of first red li:", firstRed.innerHTML);
48         let firstPurple = document.querySelector(".purple li");
49         console.log("contents of first purple li:", firstPurple.innerHTML);
50      </script>
51   </body>
52   </html>

Console Output

contents of first red li: Strawberry is yummy
contents of first purple li: Blackberry is yummy!!!