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

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -