How to use JavaScript to change the background In Javascript

This approach uses JavaScript to change the background-color after clicking the button. Use HTML DOM Style backgroundColor Property to change the background color by clicking the button. This property is used to set the background-color of an element. 

Example: This example changes the background color with the help of JavaScript

html




<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        How to change the background color
        after clicking the button ?
    </title>
</head>
 
<body style="text-align:center;">
    <h1 style="color:green;">
        w3wiki
    </h1>
 
    <h3>
        Click on button to change the
        background color
    </h3>
 
    <button onclick="myFunc()">
        Click here
    </button>
 
    <h2 id="GFG" style="color:green;"></h2>
 
    <script>
        let result = document.getElementById("GFG");
 
        function changeColor(color) {
            document.body.style.background = color;
        }
 
        function myFunc() {
            changeColor('yellow');
            result.innerHTML = "Background Color changed";
        }       
    </script>
</body>
 
</html>


Output: 

How to change the background color after clicking the button in JavaScript ?

In this article, we will learn how we can change the background color of an element once a button is clicked by the user using JavaScript and jQuery.

Table of Content

  • Using JavaScript to change the background
  • Using jQuery to change the background

Similar Reads

Using JavaScript to change the background

This approach uses JavaScript to change the background-color after clicking the button. Use HTML DOM Style backgroundColor Property to change the background color by clicking the button. This property is used to set the background-color of an element....

Using jQuery to change the background

...