JavaScript

How to change the background color with JavaScript

In this tutorial, we are going to see How to change the background color with JavaScript. You can simply use the background property to change the background color after clicking the button. This property is used to set the background color of an element.
 

How to change the background color with JavaScript
<!DOCTYPE HTML>  
<html>  
    <head>  
        <script>  
            function changeColor(color) { 
                document.body.style.background = color; 
            }
        </script>  
    </head>  
    <body style = "text-align:center;"> 
        <h1>StackHowTo.com</h1>   
        <p><b>Click on the button to change the background color.</b></p> 
        <button onclick = "changeColor('blue');">Blue</button>
        <button onclick = "changeColor('green');">Green</button> 
        <button onclick = "changeColor('red');">Red</button>
        <button onclick = "changeColor('yellow');">Yellow</button> 
    </body>  
</html>
Result

StackHowTo.com

Click on the button to change the background color.

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *