how to background button color change onclick and unclick
i tried to change the button color on onclick and unclick but not work
i tried to change the button color on onclick and unclick but not work
anyone help on this how to change button color onclick and unclick
2
Answers
To change the background color of a button on click and unclick, you can add event listeners to the button using JavaScript and change its background color using the
style.backgroundColor
property. Here is an example that changes the background color of a button to red on click and back to its default color on unclick:In this example, you first select the button using
querySelector()
. Then you define a variabledefaultColor
to store its initial background color. Next, you add an event listener for theclick
event which changes its background color to red. Finally, you add an event listener for themouseup
event which changes its background color back to the default color. You can change the colors and event types in this example to suit your needs.true
orfalse
.true
andfalse
.className
attribute to switch between a blue and red class.The implementation details of this may differ from your use-case because you may be using CSS modules or styled components, for example, but it will generally follow this pattern.