Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>The Element Object</h1> <h2>The querySelectorAll() Method</h2> <p>Add a background color to all p elements in "myDIV":</p> <div id="myDIV" style="border:1px solid black;"> <h2>A Heading</h2> <p>A p element.</p> <p>A p element.</p> <p>A p element with class="example".</p> </div> <script> const element = document.getElementById("myDIV"); const list = element.querySelectorAll("p"); for (let i = 0; i < list.length; i++) { list[i].style.backgroundColor = "red"; } </script> </body> </html>