Transforming button hues and passing a property to a subcomponent when clicked using vue.js

Just starting out with vue.js, I am attempting to create a component that functions in the following way:

I have four buttons and when I click on one button, I want to change the colors of all four buttons and send a prop to a child component indicating which button I selected.

This is what I have so far:

<template>
  <v-app>
    <v-toolbar app>
      <v-toolbar-title class="headline text-uppercase">
        <v-flex>
          <v-btn class="mx-2" icon large>
            <v-icon dark @click="reloadPage" >home</v-icon>
          </v-btn>
          <v-btn class="ma-2" :ref="ModelA" :modelProps="modelA" @click="chooseModel(0)" rounded flat text > MODEL A </v-btn>
          <v-btn class="ma-2" :ref="ModelB" :modelProps="modelB" @click="chooseModel(1)" rounded flat text > MODEL B </v-btn>
          <v-btn class="ma-2" :ref="ModelC" :modelProps="modelC" @click="chooseModel(2)" rounded flat text > MODEL C </v-btn>
          <v-btn class="ma-2" :ref="ModelD" :modelProps="modelD" @click="chooseModel(3)" rounded flat text > MODEL D </v-btn>
        </v-flex>
      </v-toolbar-title>
      <v-spacer></v-spacer>
    </v-toolbar>

    <v-content>
      <childComponent/>
    </v-content>
  </v-app>
</template>

<script>
import childComponent from './components/childComponent'

export default {
  name: 'App',
  components: {
    childComponent
  },
  data () {
    return {

    }
  },
  methods: {

    chooseModel:function(model){

      switch(model){
        case 0:
          this.$refs["ModelA"][0].color = "rgb(0, 100, 0)";
          this.$refs["ModelB"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelC"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelD"][0].color = "rgb(100, 0, 0)";
          break;

        case 1:
          this.$refs["ModelA"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelB"][0].color = "rgb(0, 100, 0)";
          this.$refs["ModelC"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelD"][0].color = "rgb(100, 0, 0)";
          break;

        case 3:
          this.$refs["ModelA"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelB"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelC"][0].color = "rgb(0, 100, 0)";
          this.$refs["ModelD"][0].color = "rgb(100, 0, 0)";
          break;

        case 4:
          this.$refs["ModelA"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelB"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelC"][0].color = "rgb(100, 0, 0)";
          this.$refs["ModelD"][0].color = "rgb(0, 100, 0)";
          break;
      }
    }

  }
}
</script>

What am I doing wrong here and what can I do to fix it?

Answer №1

Initially, keep in mind that ref is not a standard HTML attribute and therefore cannot be bound. Nevertheless, it is a custom attribute that Vue compiles, allowing you to use it as you would any other attribute.

In addition, accessing $refs['modelA'] will provide you with a reference to the Vue element, as Vue.js also encapsulates native HTML elements.

Lastly, consider storing the selected model in the Vue data and passing it as a prop to the child component.

I have prepared a jsfiddle for your reference.

https://jsfiddle.net/z3qv1dtp/

Furthermore, I suggest utilizing Vue data to store button configurations and passing them as props to each button rather than directly modifying the Vue element.

https://jsfiddle.net/z3qv1dtp/1/

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

The integration of Firebase Google Auth seems to encounter issues when used on the Vercel

My experience with Vercel deployment has been quite interesting. While I find that Google authentication works seamlessly on localhost, it seems to encounter an issue when deployed on Vercel. Specifically, after logging out, the currentUser variable always ...

Experiencing Limitations with Node.JS node-hbase Scan Functionality, Unable to Retrieve More than 1000

I am facing an issue while trying to retrieve records from an HBase table in Node.JS using the node-hbase module, which connects to the rest server. I am able to fetch the first batch of records successfully, but I am struggling to get the next set of re ...

The JSON.Parse function in Google Apps Script is leading to a 'server connection error' message during debugging

I'm not a professional developer, but I do code occasionally. Currently, I am working on some Apps Script code to interact with an API and store the data in SQL. Most of it is working fine, but I have encountered an issue while debugging in the Apps S ...

Guide to selecting a date using JavaScript in Selenium with Ruby

I'm having some trouble selecting a date from a date picker field using JavaScript in Selenium WebDriver. I've attempted the code below, which does navigate to the date window successfully, but I can't figure out how to execute the JavaScrip ...

When utilizing forEach to loop through and interact with elements in React Testing Library, the onClick event handler is not triggered. However, employing a for loop successfully triggers the

In my React project, I've created a functional component called Shop. It accepts a callback function as a prop and displays a list of items. Each item in the list triggers the callback when clicked. function Shop(props) { const { onClickMenu } = p ...

Monitoring Object Changes in Angular 4

ETA: I am aware of different methods for monitoring my form for alterations. However, that is not the focus of my inquiry. As indicated by the title, my question pertains to observing changes within an object. The application displayed below is solely for ...

Utilizing AJAX to define the attributes of an object

As a beginner in OOP, I am trying to create an object using an ajax request. My goal is to retrieve 'responseArray' in JSON format and then manipulate it. function address(adres) { this.address_string = adres; var self = this; $.ajax({ typ ...

When the div is loaded, automatically navigate to the crucial li element within it, such as the one with the class "import

Welcome to my first webpage: <html> <head> <script type="text/javascript" src="js/jquery.min.js"></script> </head> <body> <div id="comment"></div> <script type="text/ja ...

Using Jquery to preserve the selected value in a radio button

Having two radio buttons, <div class="class1"> <span><input name="chk1" type="radio" checked="checked" id="check1"/>Option 1</span> <span><input name="chk2" type="radio" id="check2"/>Option 2</span> </div> ...

Is there a way for me to retrieve the pageProbs that I have shared with a Component in _app.js?

Upon the loading of my website, I am fetching a large amount of data using getInitialProps. MyApp.getInitialProps = async (appContext) => { // Calls page's `getInitialProps` and fills `appProps.pageProps` const appProps = await App.getInitialProps( ...

The URL for Ajax is not defined

I am currently working on a function that involves fetching an XML file and making some edits to it. This is new territory for me, so I had to do some research on the best approach to accomplish this task. After some consideration, I decided to use AJAX. H ...

Delete property from object in JavaScript

Looking for a solution to replace the content of an object with another in an array of objects without passing the reference directly. The challenge is not knowing what values my function will pass, so I can't use them as keys to avoid copying the re ...

Embed Text inside an HTML Canvas

As someone who is relatively new to working with html canvas, I am having a bit of trouble when it comes to containing text within the canvas area. Specifically, I am pulling text from a textarea and displaying it on the canvas, but it seems to stay as one ...

PHP AJAX Request Failing to Transfer Files

I am having trouble with sending files in a PHP AJAX Request: Here is the form code: <form class="frmContact" action="#" method="post"> <div class="col-md-6"> <input type="hidden" name="emailTo" id= ...

Cleve js does not include delimiters in its date output

In my Vue.js project, I decided to implement a date input field using cleave.js. After passing the following options: options="{date: true, delimiter: '/', datePattern: ['m', 'd', 'Y']}" I encountered an issue wh ...

Displaying a default value in Quill text editor within Vue3

Looking at the image, you can see that I have a text editor with Quill in my admin panel. When I write something in the text editor and want to display it, everything works fine. For instance, if I want to write a description in bold, it appears on the fro ...

Node.js npm-migration enables the creation of multiple tables in a single migration process

I am new to npm-migration for nodejs and I am exploring ways to streamline the process of creating multiple tables using a single migration, rather than having separate migrations for each table creation. I have experimented with the following code: "up": ...

Looking for a solution to the issue of updating quantity in a shopping cart with Vue and

Whenever I try to increase or decrease the quantity on my shopping cart checkout page by clicking the + or - buttons, it doesn't update in real-time. However, if I navigate back to the product page and then return to the checkout page, I can see that ...

Getting data for Selectize.js

I've implemented Selectize.js to create a tagging feature for assigning users to jobs, utilizing the existing users within the system. In my scenario Following the provided documentation, I have included a select box with the multiple attribute that ...

What are the steps for setting up vue-cli on a server?

I have been researching how to install vue-cli. All the tutorials I found show it getting installed on the localhost:8080. Is it not possible to install it on a server? I attempted to install vue-cli on port 8080 on my server, but it ended up being on po ...