Javascript Change Button Color
In this tutorial, we will learn how to change the color of a button using JavaScript when it is clicked or pressed, by changing the background color dynamically using styles.
written
reviewed
updated
Thanks for your feedback!
Your contributions will help us to improve service.
How to Change Button Color in Javascript?
In this example, we use `button.addEventListener("click", function() { this.style.backgroundColor = "red"; });` to change button color on click. This code attaches a click event listener to the button element, and defines a function that changes the button's background color to red when the event occurs. This way, we can change the button color dynamically using JavaScript
Output of Javascript Button Color Change Onclick
Before Click
After Click
Ad