Switch between various states using Vue buttons

Struggling to find a solution for toggling through 4 states of button classes. I've attempted to modify some code related to "isFollowing" without success. The goal is to toggle between an off state and 3 different colors.

<button v-on:click="market(p)"
                  v-bind:class="p.isFollowing ? following : not-following"></button>


methods: {
  market: async function (symbol) {
      if (symbol.isFollowing){
        
      } else {
        //send post
      }
      this.symbol.forEach(u => {
        if (u.symbol == symbol.symbol) {
          const id = symbol.symbol
          u.symbol = "TEMP"
          u.isFollowing = !u.isFollowing
          u.symbol = id
        }
      })
  }
}


//still struggling with this:

<button v-on:click="market(p)"
                  v-bind:class="['marketButton', { 'marketButtonOff': p.marketOff, 'marketButtonGreen': p.marketGreen, 'marketButtonYellow': p.marketYellow, 'marketButtonRed': p.marketRed }]
                  "></button>

Answer №1

I designed a test element that utilizes a button click function to adjust a data attribute. This modification in data triggers an update to a calculated property that determines the button's style.

<template>
  <div class="multiple-button-states">
    <button type="button" :class="buttonClass" @click="changeButtonState">Trigger color change</button>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        buttonState: 1
      }
    },
    computed: {
      buttonClass() {
        let returnClass = ''
        switch(this.buttonState) {
          case 2:
            returnClass = 'green';
            break;
          case 3:
            returnClass = 'blue';
            break;
          case 4:
            returnClass = 'yellow';
            break;
          default:  // 1
            returnClass = 'red';
        }

        return returnClass;
      }
    },
    methods: {
      changeButtonState() {
        if (this.buttonState === 4) {
          this.buttonState = 1;
        }
        else {
          this.buttonState++;
        }
      }
    }

  }
</script>

<style scoped>
  .red {
    background-color: red;
  }

  .green {
    background-color: green;
  }

  .blue {
    background-color: blue;
  }

  .yellow {
    background-color: yellow;
  }
</style>

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

Modify CSS class if the window size is smaller than specified value

Is there a way to modify the attributes of a CSS class if the browser window is less than 600px in height? The current default properties are as follows: .side { height:400px; width:700px; } I am looking to update it to: .side { height:300px; width:550p ...

The mysterious case of the vanishing close button on Curator.io's mobile

Currently, I am using curator.io aggregator to showcase my Instagram feed on the website. An issue arises when switching to mobile view as the close button (class="crt-close") disappears. You can see an example of this here: To replicate the pr ...

Tips for sorting an array based on various criteria from a separate array

Seeking assistance with filtering results from the data array using two arrays. var data = [{"role":"Frontend", "languages": ["HTML", "CSS", "JavaScript"]},{"role":"Fullstack", ...

Linking states using AngularJS and jQuery

Imagine I have a scenario with two different states: .state('page1', { url: '/page1', templateUrl: 'pages/templates/page1.html', controller: 'page1' }) .state('page2', { url: '/page2& ...

Adjusting specific sections of a container in real-time

Fiddle: https://jsfiddle.net/1b81gv7q/ Sorry for the slightly cryptic title; I couldn't come up with a better way to phrase it. Imagine this scenario: there's a container with content that needs to be dynamically replaced. If I wanted to repla ...

res.send() triggers an error of TypeError: Attempting to convert circular structure to JSON

I've encountered an error message that has been discussed before, but none of the proposed solutions seem to work for my situation. My current project involves building a backend express server to manage API requests for a React application. I have c ...

Tips for efficiently passing a JSON object to a resource using AngularJS

I am looking to integrate a web service that accepts a JSON object with two arrays in a POST request and responds with a JSON object array. Now, I want to make a request to this service from my AngularJS application. Below is the code snippet: wellApp.fa ...

What is the best way to eliminate padding: 0; in a sass file?

Currently, I am focusing on enhancing the responsiveness of the footer section on my website. To achieve this, I have implemented a media query that triggers when the screen size is reduced to less than 992px. This allows the content within the footer to b ...

Struggling to maintain context with axios in React despite diligent use of arrow functions

In my component, I have a function for posting data. Although it works well, the context of my component is lost in the success message. This is puzzling because I am using arrow functions. Why does the "this" context get lost in this situation? The issu ...

Navigating the browser view across an extensive HTML page without relying on scrollbars

I have a large HTML page (approximately 7000x5000) and I need to be able to move the user's view around with JavaScript. Disabling the ability for the user to scroll by hiding the browser scrollbars using overflow:hidden on the HTML element is easy. ...

avoidable constructor in a react component

When it comes to specifying initial state in a class, I've noticed different approaches being used by people. class App extends React.Component { constructor() { super(); this.state = { user: [] } } render() { return <p>Hi</p> ...

Trying out the Deezer app and receiving the message: "Please provide a valid redirect URI."

While testing an application using the Deezer JavaScript SDK, I encountered an issue when trying to login as it prompted me with a "You must enter a valid redirect uri" message. Here is the setup: DZ.init({ appId: '000000', channelUrl: ...

Vue.js filtering by number

Looking to implement filtering in a Vue project. I have an input field that currently filters by store name extracted from a JSON object, which includes both the store name and ID as a number. How can I also filter by store ID? computed: { filtered ...

Extension for Chrome browser

I am new to creating Chrome extensions and I am attempting to build one that will display the IDs of all elements with a specific class name on a website in the popup window. I would like to know if there is a better way to tackle this issue. Thank you for ...

The mat-table's data source is failing to refresh and display the latest

When I click on a column header to sort the table, a function should trigger and update the data. However, the data is not updating as expected. Below is the code for the function and the table: <table mat-table #table [dataSource]="dataSourceMD&qu ...

Ways to call a method in a grandparent from a grandchild

I am stuck on what to name this particular scenario. I have a file, which we'll refer to as parent.vue. It contains the following: PARENT.vue <template> <input-box :room="currentRoom" v-on:messagesent="getMessages&qu ...

Utilizing diverse values retrieved from HTML data attributes

*UPDATE Within my HTML, I have a list titled "#wordlist" that contains the words for my game, along with corresponding audio and images for each word. Everything is functioning correctly. As there will be multiple versions of the game, I've been tas ...

Sending a JavaScript function as a value in a JSON object

I've been working on incorporating a word cloud with click event handler using jQCloud. To achieve this, I need to include a JavaScript function within JSON. Within C#, I have generated the dynamic JSON text: foreach (var r in result) { sbChart. ...

Error: The session ID is not currently active

I encountered a problem with my tests failing when running them on TFS, showing the following error: WebDriverError: No active session with ID Failed: No active session with ID Interestingly, these same tests are passing locally. Everything was working ...

The essential guide to creating a top-notch design system with Material UI

Our company is currently focusing on developing our design system as a package that can be easily installed in multiple projects. While the process of building the package is successful, we are facing an issue once it is installed and something is imported ...