Document querySelectorAll() Method - Javascript DOM

Javascript examples for DOM:Document querySelectorAll

Description

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 Values

Parameter Type Description
CSS selectors String Required.

Return Value:

A NodeList object.

The following code shows how to Get all elements in the document with class="example":

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials