Javascript examples for DOM:Element querySelectorAll
Element querySelectorAll() Method - Set the background color of all <p> elements in a <div> element:
<!DOCTYPE html> <html> <head> <style> #myDIV {/*from ww w . ja va 2s .c o m*/ border: 1px solid black; margin: 5px; } </style> </head> <body> <div id="myDIV"> <h2>A heading in div</h2> <p>A p element in div.</p> <p>Another p element in div.</p> <p>A p element with class="example" in div.</p> </div> <button onclick="myFunction()">Test</button> <script> function myFunction() { var x = document.getElementById("myDIV").querySelectorAll("p"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } } </script> </body> </html>