Friday, June 21, 2024

button change color using javascript source code

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

Change H1 text using buttons dynamically Javascript Sourcecode

Javascript Task How can i make it so that when a user clicks a button, the text from that button is shown...