Vue Js Change Button Color On Click
Vue Js Change Button Color On Click | Css | Example. To change a button's color on click event, you can use JavaScript to add an event listener to the button element. Within the event listener, you can use the style property to set the button's background color to the desired color. Alternatively, you can create two classes with different background colors and toggle between them on click event using the classList property. This approach allows you to easily switch between different styles without directly manipulating the style property. In both cases, you can use CSS to define the button's initial style and the colors to switch between.
Thanks for your feedback!
Your contributions will help us to improve service.
Vue Js Change Button Color On Click | CSS
This code uses Vue 3 to create a simple button that toggles between two different background colors. It does this by defining a Vue component with a data
object containing a btnClass
property set to "red-color". The button's class is bound to this property using Vue's :class
directive.
When the button is clicked, the toggleColor
method is called and it checks the current value of btnClass
. If it's "red-color", it sets it to "yellow-color" and vice versa. This updates the button's class and changes its background color accordingly.
The CSS styles define the button's appearance, including its padding, text color, and border. The .my-btn
class is applied to the button element directly, while the .red-color
and .yellow-color
classes are used to define the background colors that the button toggles between.
Using above example you can toggle button color.
Output of above example will look something like this -
After Button Click it will look like as below -