I want my Button change color every time I click on it. But it changes color only at the first press.
I believe the problem is with the setColor function. Each time I click on Button , count gets the value 1. Thus, even when I set it to 0, it gets reset to 1 the next click. How to fix it? Are there global variables in javascript / html where this is easy to solve?
<!DOCTYPE html> <html> <head> <script> function setColor(btn, color){ var count=1; var property = document.getElementById(btn); if (count == 0){ property.style.backgroundColor = "#FFFFFF" count=1; } else{ property.style.backgroundColor = "#7FFF00" count=0; } } </script> </head> <body> <input type="button" id="button" value = "button" style= "color:white" onclick="setColor('button', '#101010')";/> </body> </html>
javascript html button onclick global
user2456977
source share