Vuetify Change Button Color if Button active or selected

In this tutorial, we will explore how to change the color of Vuetify buttons when they are active or selected. We'll be using Vue.js and Vuetify to create a simple interface with three buttons. The button that is clicked will change color to indicate its active state. This functionality is achieved by dynamically adding a CSS class to the buttons

Profile Photo

Abhishek Yadav (SD) SDE of FAI

Feedback

written
Profile Photo

Anil Kumar (Expert) Coding Expert of FAI

Feeback

reviewed
Profile Photo
Nov 02, 2023 02:11 AM Last Updated
updated

How to Change Button Color in Vuetify When the Button is Active or Selected?

The HTML Structure consists of a Vuetify container containing three buttons, each with a click event listener. The buttons have a default primary color and a dynamic class binding to change their color when they are active

Changing Vuetify Button Color When Active or Selected

Copied to Clipboard
Run

In the JavaScript code, we use Vue.js and Vuetify to create our application. We set up a Vue app and define the 'activeButton' data property, initialized to 'null'. This property stores the currently active button number. We also define a method called 'changeActiveButton', which is called when a button is clicked.

The JavaScript Code

Copied to Clipboard

The 'changeActiveButton' method takes a 'buttonNumber' argument to identify which button was clicked. If the clicked button is already active, we set 'activeButton' to 'null' to deactivate it. If a different button is clicked, we update 'activeButton' with the 'buttonNumber', effectively deactivating the previously active button and activating the newly clicked one.

By using the ':class' directive, we apply the 'active-button' CSS class to the buttons when 'activeButton' is equal to their respective button numbers. This class is responsible for changing the button's color when active.

Pasted Image

Output of Vuetify Change Button Color If Button Active Or Selected 

This tutorial demonstrated how to change the color of Vuetify buttons when they are active or selected. By combining Vue.js data binding and Vuetify's ':class' directive, you can create a visually appealing interface that provides user feedback when buttons are clicked. This simple and effective technique can be applied to a variety of Vue.js and Vuetify projects.

Ad