Change Button Color OnClick
Answer : There are indeed global variables in javascript. You can learn more about scopes, which are helpful in this situation. Your code could look like this: <script> var count = 1; function setColor(btn, color) { var property = document.getElementById(btn); if (count == 0) { property.style.backgroundColor = "#FFFFFF" count = 1; } else { property.style.backgroundColor = "#7FFF00" count = 0; } } </script> Hope this helps. 1. function setColor(e) { var target = e.target, count = +target.dataset.count; target.style.backgroundColor = count === 1 ? "#7FFF00" : '#FFFFFF'; target.dataset.count = count === 1 ? 0 : 1; /* () : ? - this is conditional (ternary) operator - equals if (count === 1) { target.style.backgroundColor = "#7FFF00"; target.dataset.count = 0; } else { ...