Javascript Task
How can I create an HTML button that, when clicked, triggers a Javascript function, and changes the button's color to red with each successive click?
Tips: use document.getElementById().style.backgroundColor
<button id="btn1" onclick="change()">change color</button> <script> var btn1=document.getElementById("btn1"); var boo=false; function change() { if(boo){ btn1.style.backgroundColor="red"; boo = !boo; return; } if(!boo){ btn1.style.backgroundColor="white"; boo = !boo; return; } } </script>
No comments:
Post a Comment