0
Hello, in the following exercise, there should be 3 Buttons, and clicking on each one, will change the body color by the color indicated on the button (using the same function for all), however my attempt was failed, I would like to know why, follows down the statement of the exercise and my attempt:
1)Elements must change the background color of the page when clicked according to its color. Present the javascript needed for this to work.
button id="blue">Blue
button id="green">Green
button id="red">Red
html:
<html>
<head>
<meta charset = "utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href = "./css/style.css">
<title>Meu Site</title>
</head>
<body>
<main>
<button id=”azul” onclick="trocarcor(id)">Azul</button>
<button id=”verde” onclick="trocarcor(id)">Verde</button>
<button id=”vermelho” onclick="trocarcor(id)">Vermelho</button>
</main>
<script src="./scripts/app.js"></script>
</body>
Javacript:
function trocarcor(id) {
if (id == "vermelho") {
document.getElementsByTagName("body")[0].style.background = "red"
}
else if(id == "azul"){ document.getElementsByTagName("body")[0].style.background = "blue"}
else if(id == "verde"){ document.getElementsByTagName("body")[0].style.background = "green"}
console.log(id)}
What he’s printing on
console.log
of id?– Felipe Avelar
is printing the respective colors of each button, I did to confirm that the id was receiving "red", "green" and "blue"
– gmtlme