Issue with Vue class binding failing to update when state is modified

I'm attempting to utilize Vue class binding depending on the index within the v-for loop. While I can see that the state in Vue dev tools is changing correctly, the class name itself isn't being updated.

<div class="group" v-for="(role, index) in roles_be">
  <span class="group-name"> {{ role.name }}</span>
  <i class="fas fa-info-circle"></i>
  <i class="fa fa-fw switch-icon" @click="toggleGroupElements(index)" v-bind:class="[ 
  groupElements.index ? 'fa-toggle-on' : 'fa-toggle-off' ]"></i>
</div>


groupElements: [false, false, false, false],


toggleGroupElements(index) {
              switch (index) {
                case 0:
                  this.groupElements[0] = !this.groupElements[0];
                  break;
                case 1:
                  this.groupElements[1] = !this.groupElements[1];
                  break;
                case 2:
                  this.groupElements[2] = !this.groupElements[2];
                  break;
                case 3:
                  this.groupElements[3] = !this.groupElements[3];
                  break;
              }

            },

Answer №1

Instead of attempting to retrieve a key in an object, you should use groupElements[index] for the class binding as you are accessing an index in your array.

:class="[groupElements[index] ? 'fa-toggle-on' : 'fa-toggle-off' ]"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating the layout in Vue.js without the need for a full page refresh

My goal is to have the Dialog expand to fullscreen when the screen size is small. The current code I am using is as follows: <v-dialog max-width="600px" :fullscreen="screen_width < 450 ? true : false"> ... screen_width: screen.width U ...

Guide on deactivating a button when a numerical value is detected within a field in VUE JS

I need help figuring out how to automatically disable a button when a field contains a number. Here is an example of my code: disabledSubmitButton() { return this.$v.$error || this.firstName === '' || this.lastName === '' || ...

Obtain the selected values from Vuetify.js checkboxes

Recently, I've been working on a project that involves fetching values from a PostgreSQL database using postgREST and displaying them in a table with Vuetify. I want to include checkboxes next to each value so users can delete rows from the table. Th ...

Does the String.replace() function in Javascript have the capability of incorporating line breaks?

Is it possible to use the String.replace() method in JavaScript to replace any character with a line feed symbol? For example: newString = oldString.replace(/x/, "linefeed character (\n)"). ...

Prevent Child Elements from Overstretching Container with Bootstrap Flex

I'm working on a layout where all elements can be viewed without the need for scrolling. Any panels that require more space should be scrollable. In the following example, I want the contents of main-left to be scrollable without stretching the entire ...

Is it possible to achieve a seamless interaction by employing two buttons that trigger onclick events to alternately

I have a specific goal in mind: I want to create two buttons (left and right) with interactive cursors. The left button should initially have a cursor of "not-allowed" on hover, indicating that it cannot be clicked at the beginning. When the right button ...

Is it possible to generate a "pop-up" window upon clicking on the register button?

As a backend programmer, I'm looking to create a popup window that appears in front of the current window when users click "register", eliminating the need for redirection to another page. I believe you understand the concept. How can I achieve this? ...

Tips for adjusting UI with Media queries based on screen dimensions

Looking for a better solution I currently have 5 sets of items. Button Text Dropdown Pagination Icon For larger screen sizes, specifically @media (min-width: 990px) {} I would like all items to be in a single row, like so: [button][Text][Dropdown][Pagi ...

Failure to trigger a follow-up function in webSQL transaction's success callback

Review the code below. I have called setQuestion() within the successCallBack of db.transaction but am encountering an error: Uncaught TypeError: this.setQuestions is not a function. Can you spot any issues in my code? game.module( "game.scenes.scene" ) ...

Cypress: Uncovering the method invoked by a button click

I'm currently utilizing Vue3 with Vite and Cypress. My Vue3 component utilizes the script setup SFC syntax. Below is the code snippet for my component: <template> <div> <button data-cy="testBtn" @click="btnClick()&q ...

What is the method to define a loosely typed object literal in a TypeScript declaration?

We are currently in the process of creating TypeScript definitions for a library called args-js, which is designed to parse query strings and provide the results in an object literal format. For example: ?name=miriam&age=26 This input will produce th ...

Changing the close button icon in highslide popups

Utilizing highslide together with highcharts, I need to customize the functionality of the close button. Specifically, I want to trigger an additional function when a user clicks on the "X" button. Upon inspecting the "X" button, this is what appears in m ...

Using Selenium WebDriver and JavaScript: Enabling Chrome to Download Multiple Files at Once

After scouring through multiple documents for hours like https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/ as well as https://chromedriver.chromium.org/capabilities and I was unsuccessful in finding a solution wit ...

"Running experiments with Google Ads and Google Analytics scripts results in a blank page being displayed

Currently, I am working on a plain HTML file and conducting tests to ensure that the Google Ads and Analytics scripts are functioning correctly before implementing them on other pages. A colleague who has an AdSense account provided me with the script code ...

The error "Cannot set headers after they are sent" is causing issues with the functionality of the Express session

Ensuring secure authentication for my Node.js application is a top priority. I have implemented the use of express-session npm to achieve this goal. The idea is that upon successful login on the /login page, a session should be initiated and the user shoul ...

The appearance of the circle in Safari is rough and lacks smoothness

My circle animation works perfectly on Google Chrome, but when I switch to Safari the edges appear faded and blurry. I tried adding "webkit" to fix it, but had no luck. Is there a compatibility issue with Safari and CSS animations? Here is my code: Snapsh ...

Using Vue Router to set a variable as a key for an object in JavaScript

In my current project, I am attempting to pass a variable as the parameter to vue-router in order to dynamically set it. The code snippet below demonstrates what I am trying to achieve: <router-link :to="{ name: notification.name, (notification.pa ...

NodeJS has a knack for replying even before the function has completed

Struggling with a NodeJS and Express API for a school project. The getAuthUserId function is not working as expected. It decodes the JWT token to retrieve the user Id from the mongoDB server. However, when calling this function in a REST call "/user/authT ...

Numerous pop-up windows displaying a variety of distinct content in each one

While working on a website, I came across a helpful jQuery pop-up function that I found on Stack Overflow. The original post can be found here. I am trying to customize each pop-up box so they contain different content from the .pop1 and .pop2 divs (parag ...

Extracting data from the Vue object variables within a component

I'm brand new to Vue and I'm struggling with accessing data in the main Vue instance from a component. In my Vue instance, I have: var vm = new Vue({ computed: {}, data: { show: true }, And in the component, I'd like to d ...