'querySelectorAll' does not work

2

I was doing a test that when clicking the button all 'p' should turn red. Does anyone know what's wrong?

function test() {
	document.querySelectorAll('p').style.color = 'red';
}
<!DOCTYPE html>
<html>
<head>
	<title>ola</title>
	<meta charset="utf-8">
</head>
<body>
	<h1>teste</h1>
	<br>
	<hr>
	<p>ola</p>
	<p>oi</p>
	<p>qwerty</p>
	<button onclick="test()">testes</button>
	<script src="teste.js"></script>
</body>
</html>
    
asked by anonymous 17.07.2017 / 17:27

1 answer

4

As the friend had said above, to work, you will have to do so ...

let elemento = document.querySelectorAll('p')

// Pega apenas o primeiro do array
elemento[0].style.color = 'red';

// pega todos

for(let i = 0; i < elemento.length; i++){
    elemento[i].style.color = 'red';
}
<h1>teste</h1>
	<br>
	<hr>
	<p>ola</p>
	<p>oi</p>
	<p>qwerty</p>
    
17.07.2017 / 17:38