Javascript examples for DOM:Document querySelectorAll
The querySelectorAll() method selects elements by a specified CSS selector(s) and returns NodeList object.
It throws a SYNTAX_ERR exception if the selector(s) is invalid
Parameter | Type | Description |
---|---|---|
CSS selectors | String | Required. |
A NodeList object.
The following code shows how to Get all elements in the document with class="example":
<!DOCTYPE html> <html> <body> <h2 class="example">A heading with class="example"</h2> <p class="example">A paragraph with class="example".</p> <button onclick="myFunction()">Test</button> <script> function myFunction() {/*from www. j a v a2 s .c o m*/ var x = document.querySelectorAll(".example"); x[0].style.backgroundColor = "red"; } </script> </body> </html>