HTML DOM querySelectorAll() Method

HTMLWeb DevelopmentFront End Technology

The HTML DOM querySelectorAll() method returns all elements that matches a specific CSS selector specified in its parameter in an HTML document.

Syntax

Following is the syntax −

document.querySelectorAll(“CSS Selector”);

Example

Let us see an example of querySelectorAll() method −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   html{
      height:100%;
   }
   body{
      text-align:center;
      color:#fff;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
      height:100%;
   }
   .drop-down{
      width:35%;
      border:2px solid #fff;
      font-weight:bold;
      padding:8px;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
   .show{
      font-size:1.5rem;
      font-weight:bold;
   }
</style>
</head>
<body>
<h1>DOM querySelectorAll() Method Demo</h1>
<p class="para">Hi! I'm a paragraph with some random text.</p>
<p class="para">This is a awesome paragraph.</p>
<p class="para">I'm a &lt;p&gt; element in HTML</p>
<button onclick="changeColor()" class="btn">Change all para styles</button>
<script>
   function changeColor() {
      var paraElements = document.querySelectorAll(".para");
      paraElements.forEach((paraElement)=>{
         paraElement.style.color="#db133a";
         paraElement.style.fontSize="1.2rem";
      });
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on “blue” button to change styles of all paragraph elements.

raja
Published on 19-Jul-2019 13:40:26
Advertisements