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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -