+ 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

21st Apr 2018, 4:50 AM
Ceci Alejo
Ceci Alejo - avatar
4 Antworten
+ 10
not only style.backgroundcolor you can use style.background
23rd Apr 2018, 11:52 AM
Ahlin Chan
Ahlin Chan - avatar
+ 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>
23rd Apr 2018, 9:54 AM
Ahlin Chan
Ahlin Chan - avatar
+ 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.
23rd Apr 2018, 6:34 AM
Dev
Dev - avatar
+ 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
23rd Apr 2018, 11:49 AM
Ceci Alejo
Ceci Alejo - avatar