javascript - If statements with Background Colors -
hi wanted see going wrong these if statements cant see wrong them, not seem work intended switch color when html button clicked sure working console displays "button clicked" when clicking button.
function colorswitch() { var thediv = document.getelementsbyclassname("light"); console.log("button clicked"); if(thediv[0].style.backgroundcolor == "#ff0000") { thediv[0].style.backgroundcolor = "#ffff00"; } if(thediv[0].style.backgroundcolor == "#ffff00") { thediv[0].style.backgroundcolor = "#00ff00"; } if(thediv[0].style.backgroundcolor == "#00ff00") { thediv[0].style.backgroundcolor = "#ff0000"; } };
you need add keyword else
, otherwise each condition being met, making go full circle.
function colorswitch () { var thediv = document.getelementsbyclassname("light"); console.log("button clicked"); if (thediv[0].style.backgroundcolor == "#ff0000") { thediv[0].style.backgroundcolor = "#ffff00"; } else if (thediv[0].style.backgroundcolor == "#ffff00") { thediv[0].style.backgroundcolor = "#00ff00"; } else if (thediv[0].style.backgroundcolor == "#00ff00") { thediv[0].style.backgroundcolor = "#ff0000"; } };
Comments
Post a Comment