+ 2
[HELP] click me to change color not working
nothing is happening when i press the click me button and no error shows up. please help me im a beginner thank you :) https://code.sololearn.com/Whj7gmk9GR78/?ref=app
4 Respuestas
+ 10
not only style.backgroundcolor
you can use style.background
+ 9
Ked Alejo
using if..else to check if the color green new color well be red
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="parentBox1">
<div id="childBox1"></div>
<button id="button1" onclick="clickme()">click me</button>
</div>
<script>
var childB = document.getElementById("childBox1")
function clickme() {
if(childB.style.backgroundColor == "red"){
childB.style.backgroundColor = "green";
}else{
childB.style.backgroundColor = "red"
}
}
</script>
</body>
</html>
+ 6
It is working as expected, as far as I see it. It is not visible because the parent div has green color which matches with it. Change the color, to see it distinctly.
+ 2
Thank you guys for answering.
U could not see the error cause i have already fixed it.
it is because of my old code
x.style.color
i learned that the correct one is .backgroundColor and not .color because it is just for text