So far we have limited ourselves to using method that returned a
single element.
But, we may want to select
multiple HTML elements and this is where
document.querySelectorAll() and arrays come in. You can use
CSS Selectors as an argument to this method
to select multiple HTML Elements. The result is returned as an array.
var element01 = document.querySelectorAll(".example"); // selects all HTML Elements with class "example"
var element02 = document.querySelectorAll("p"); // selects all p element
var element03 = document.querySelectorAll("div > p"); // selects all p element that is child of div
You can do this but...
element01[0].style.backgroundColor = "yellow";
element01[1].style.backgroundColor = "blue";
Full Example (Not good manual traversal of arrays):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> The Title of this HTML Page </title>
</head>
<body>
<p>paragraph 0</p>
<div class="example">
<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>
</div>
<div class="example">
<p>paragraph 4</p>
<p>paragraph 5</p>
<p>paragraph 6</p>
</div>
<button onclick="select_all_p();">select all p</button>
<script>
// called when second button is clicked
function select_all_p(){
// selects the first p element
var array_of_p = document.querySelectorAll("p");
array_of_p[0].style.color = "green";
array_of_p[1].style.color = "green";
array_of_p[2].style.color = "green";
}
</script>
</body>
</html>
this is not robust coding. You will get an error if the list is empty
and you need to know/count exactly how many there are in the list manually.
Full Example using forEach
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> The Title of this HTML Page </title>
</head>
<body>
<p>paragraph 0</p>
<div class="example">
<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>
</div>
<div class="example">
<p>paragraph 4</p>
<p>paragraph 5</p>
<p>paragraph 6</p>
</div>
<button onclick="select_all_p();">select all p</button>
<script>
function changecolor(p){
p.style.color = "green";
}
// called when second button is clicked
function select_all_p(){
// selects the first p element
var array_of_p = document.querySelectorAll("p");
array_of_p.forEach(changecolor);
}
</script>
</body>
</html>
Let's look at this in a full example
[
here]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> The Title of this HTML Page </title>
</head>
<body>
<p>paragraph 0</p>
<div class="example">
<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>
</div>
<div class="example">
<p>paragraph 4</p>
<p>paragraph 5</p>
<p>paragraph 6</p>
</div>
<button onclick="select_example();">select all members of class example</button>
<button onclick="select_all_p();">select all p</button>
<script>
// the most cryptic but fastest funciton to generate random colors
// in the form of hex numbers #000000 --> #FFFFFF
function getRandomColor(){
return '#' + ('00000'+(Math.random()*(1<<24)|0).toString(16)).slice(-6)
}
function changebackground(e){
e.style.backgroundColor = getRandomColor();
}
// this gets caleld when first button clicked
function select_example(){
// selects the All HTML Element with class "example" and assigns the array to a variable named array01
var array01 = document.querySelectorAll(".example");
// for each element in the array, apply function changebackground
// with the individual element as an argument
array01.forEach(changebackground);
}
</script>
</boody>
</html>