Set the background color of all <p> elements in the document:
Click the button to change the background color of all p elements in the document.</p>
<!DOCTYPE html> <html> <head> <style> .example {// w w w . j a v a 2s .c o m border: 1px solid black; margin: 5px; } </style> </head> <body> <h1>A h1 element</h1> <div>A div element</div> <p>A p element.</p> <p>Another p element.</p> <div class="example"> <p>A p element inside a div element.</p> </div> <button onclick="myFunction()">Test</button> <script> function myFunction() { var x = document.querySelectorAll("p"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } } </script> </body> </html>