Javascript examples for DOM:Element querySelectorAll
The querySelectorAll() method returns child elements by CSS selector(s) as a static NodeList object.
The NodeList is a static collection, changes in the DOM has NO effect in the collection.
It throws a SYNTAX_ERR exception if the specified selector(s) is invalid
Parameter | Type | Description |
---|---|---|
CSS selectors | String | Required. |
A NodeList object, representing all child elements that matches a specified CSS selector(s).
The following code shows how to Set the background color of the first element with class="example" inside of a <div> element:
<!DOCTYPE html> <html> <head> <style> #myDIV {/*from w w w . j a v a 2 s . c o m*/ border: 1px solid black; margin: 5px; } </style> </head> <body> <div id="myDIV"> <h2 class="example">A heading with class="example" in div</h2> <p class="example">A paragraph with class="example" in div.</p> </div> <button onclick="myFunction()">Test</button> <script> function myFunction() { var x = document.getElementById("myDIV").querySelectorAll(".example"); x[0].style.backgroundColor = "red"; } </script> </body> </html>