How to change the Background Color after Clicking the Button in JavaScript? Last Updated : 30 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Changing the background color after clicking a button in JavaScript involves attaching a click event listener to the button. When the button is clicked, it triggers a function that changes the background color of a specific element (like the page or a section of it).This creates a simple and interactive effect that enhances the user experience.ApproachesBelow are the following approaches by which we can change the background color after clicking the button in JavaScript.1. Using JavaScriptUsing JavaScript to change the background color involves adding an event listener to a button, which triggers a function to set the background color property of an element, dynamically altering the background when the button is clicked.Example: In this example, we define changeColor to modify the background color, and myFunc to set the background to yellow and update the <h2> text. The button click triggers myFunc(). html <html> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </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:In this Example:The page displays a heading, a subheading, and a button using basic HTML elements.The <button> has an onclick attribute that runs the myFunc() function when clicked.The changeColor(color) function sets the page’s background color using document.body.style.background.The myFunc() function calls changeColor('yellow') to change the background to yellow.It also updates the <h2> element (with id="GFG") to display the message "Background Color changed".The JavaScript logic is placed inside a <script> tag at the bottom of the HTML body.2. Using jQueryThis approach uses jQuery to change the background color after clicking the button. Below are the methods and their uses that will be used in this approach.The text() method is used to set the text content of the selected element.The on() method is used as event handlers for the selected elements and child elements.The css() method is used to change/set the background color of the element.Example: This example changes the background color with the help of JQuery. html <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Click on button to change the background color </h3> <button> Click here </button> <h2 id="GFG" style="color:green;"></h2> <script> $('button').on('click', function () { $('body').css('background', '#ccc'); $('#GFG').text("Background Color Changed!"); }); </script> </body> </html> Output:In this example:The code loads the jQuery library from a CDN (ajax.googleapis.com) to enable easier manipulation of HTML elements and events using jQuery functions.It creates a webpage with centered content, displaying a heading “GeeksforGeeks”, a subheading, a clickable button, and an empty heading (<h2>) with the ID "GFG".When the button is clicked, a jQuery function runs that performs two actions.The background color of the entire <body> is changed to light gray (#ccc) using jQuery's .css() method.The empty <h2> with ID "GFG" is updated to show the text “Background Color Changed!” using jQuery’s .text() method.ConclusionChanging the background color of a webpage after clicking a button is a simple but effective way to add interactivity to your site. By using JavaScript and the jQuery you can respond to user actions and modify the style of your page dynamically. Comment More infoAdvertise with us P PranchalKatiyar Follow Improve Article Tags : JavaScript JavaScript-Questions Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an 7 min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav 15+ min read Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex 4 min read Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax 5 min read JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva 3 min read HTML DOM (Document Object Model) The HTML DOM (Document Object Model) is a programming interface that represents the structure of a web page in a way that programming languages like JavaScript can understand and manipulate. Think of it as a tree of objects where each part of your HTML document (elements, attributes, text) is repres 6 min read Like