Changing the color of an input field in Vue when the delete button is clicked

I have recently started working with Vue.js and I am attempting to change the color of an input field when the user clicks on the trash-fill button. Currently, when I enter a character in the input field, it changes the color to green.

Is there a way to change the input field color from green to white when the user clicks on the

<b-icon icon="trash-fill" font-scale="1.5" @click="deleteRfidBeforeReload($event, index, 10)"></b-icon>
?

View

<div id="app">
  <div v-for="(listings, index) in list10" :key="index">
   <b-row>
     <b-col sm="6">
       <b-form-input id="input-live" :value="listings.first_name" disabled></b-form-input>
     </b-col>
     <b-col sm="4">
       <b-form-input v-model="listings.rfidState1" ref="todos" v-on:input="posttorfidapi($event, 10, index)" 
:style="listings.rfidState1 ? { 'background-color': '#33FF90', color:'#33FF90' } : null"></b-form-input>
     </b-col>
      <b-col sm="2">
        <b-icon icon="trash-fill" font-scale="1.5" @click="deleteRfidBeforeReload($event, index, 10)"></b-icon>
      </b-col>
    </b-row>
  </div>
</div>

Script

new Vue({
  el: "#app",
  data: {
    list10: [
      { first_name: "mission1", id: "1", rfidState1:""},
      { first_name: "mission2", id: "2", rfidState1:""},
      { first_name: "mission3", id: "3", rfidState1:""},
      { first_name: "mission4", id: "4", rfidState1:""}
    ]
  },
  methods: {
    posttorfidapi(event, col, index){
        if(event.length > 3){
        console.log("CHANGE INPUT FIELD COLOR TO GREEN");
      }
    },
    deleteRfidBeforeReload($event, index, col){
        console.log(index);
      console.log("CHANGE THE PARTICULAR INPUT FIELD TO WHITE AGAIN");
    }
  }
})

My code on JSFIDDLE

https://jsfiddle.net/ujjumaki/7qsnvftm/17/

Answer №1

It seems like you're utilizing the ternary operator in your code. One suggestion is to add a new property to each object in your array, such as whiteBackground, which can be toggled between true and false. You can set it to true when a button is clicked and back to false when an input changes. This additional property can then be included in another ternary operation along with your existing one. Here's an example:

  list10: [
    {
      first_name: "mission1",
      id: "1",
      rfidState1: "",
      whiteBackground: false
    },
    //...
  ]

Methods:

posttorfidapi(event, col, index) {
    this.list10[index].whiteBackground = false;
    console.log("CHANGE INPUT FIELD COLOR TO GREEN");
  }
},
deleteRfidBeforeReload($event, index, col) {
  this.list10[index].whiteBackground = true;
  console.log("CHANGE THE PARTICULAR INPUT FIELD TO WHITE AGAIN");
}

And for the ternary condition:

:style="listings.rfidState1 ? listings.whiteBackground ? '' : 
   { 'background-color': '#33FF90', color:'#33FF90' }  : ''"

CODESANDBOX

By the way, your fiddle isn't functioning properly...

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

Learn the steps to retrieve a user's profile picture using the Microsoft Graph API and showcase it in a React application

I'm currently working on accessing the user's profile picture through Microsoft's Graph API. The code snippet below demonstrates how I am trying to obtain the profile image: export async function fetchProfilePhoto() { const accessToken = a ...

Encountered an unexpected token '{' error in Discord.js and Node.js integration

let user = message.mentions.users.first(); if (message.mentions.users.size < 1) return message.reply('Please mention someone to issue ARs.').catch(console.error); mcash[${user.id}, ${message.guild.id}].mc ...

Restrict user uploads to a maximum of 5 posts per minute using Jquery

Recently, I have implemented this jQuery/AJAX code snippet: var uploaded=0; if (uploaded!=0) { setInterval(function() { uploaded--; }, 60000); alert(uploaded); } $(".UploadMSub").click(function(event){ event.preventDefault(); ...

How to create a clickable link using Vuetify's v-btn component

As a newcomer to vue and vuetify, I would greatly appreciate some explanation and examples. My goal is to create a button that directs users to an external site, like youtube.com. Below is the code I currently have, but unfortunately it's not function ...

JavaScript and HTML are commonly used programming languages for developing

By utilizing JavaScript, I was able to generate a table dynamically based on user input. For example, if the user enters 3 and clicks "go", a table with 3 rows is created. Using the .keyup function allowed me to target a column successfully. However, an i ...

Connecting JavaScript and jQuery scripts

Help needed! I am a beginner in the world of jQuery and JS. Unfortunately, my JS/jQuery code is not running and I can't figure out why. Can someone please take a look at my HTML and guide me on what might be causing the issue? Do I need to add some ad ...

Unsynchronized Request Sequencing

Seeking advice on enhancing a previously accepted answer related to chained ajax requests can lead to some interesting solutions. One proposed solution involves sequencing 3 ajax requests in an asynchronous chain: var step_3 = function() { c.finish() ...

The Reactjs infinite scroll feature continuously displays fresh data

Every time I scroll, my data is always rendering on page 2 and page 1 disappears, and the items.concat function is not working properly. My fetch data code looks like this: const FetchUrl = async (p) =>{ const Url = await fetch(`api-link=${p}`); ...

Elements that possess identical class attributes yet exhibit dynamic behavior

I'm currently facing challenges with dynamically generated elements. I am working on a page for my website that will show a list of users (data provided by the controller). Each user is represented within a div, containing two h3 tags displaying the u ...

Do we really need to create our own action creators in Redux Toolkit?

As I delve into implementing the redux toolkit in my react projects, I've come up with a structure for writing slices using redux-thunk to handle API requests. import { createSlice } from "@reduxjs/toolkit"; import axios from "axios&quo ...

Iterating through div elements and assigning unique ids to them before enabling click functionality

I'm attempting to create multiple div elements inside my loop that will each display a unique message when clicked. However, I'm encountering issues with the code and can't seem to make it work as intended. Here is what I am trying to achiev ...

What is the most effective method for incorporating personalized React components in the midst of strings or paragraph tags

Summary: Exploring the frontend world for the first time. Attempting to integrate custom components within p-tags for a website, but facing challenges in making them dynamically changeable based on user interaction. Greetings all! As a newbie in front-end ...

Currently waiting for the $resolved property of the AngularJs service

Here's a straightforward issue with what I hope is an equally simple solution. I've set up multiple services for handling CRUD operations with tags. myApp.factory('GetTags', ['$resource', function ($resource) { return $re ...

Multi-line input in ExtJs

Is there a way to create a multiline input with vertical scrollbars in EXTJS? I tried the following code: noteField = new Ext.form.TextField({ emptyText: 'note...', multiline: true, applyTo: &apos ...

Decoding arrays of JSON data

Receiving "chunked" data (json arrays) on the front-end via "xhr" (onprogress). Handling delayed chunks is easy - just remember response length and offset. The challenge comes when multiple "chunked" responses arrive simultaneously, resulting in an unpars ...

Getting a hold of an Array value from an external scope in Angular JS

I've been struggling with this issue for a while and can't seem to find a solution. I have a code snippet where I'm trying to pass values from getJSON to an angular controller, but my array loses its values in the process. Can someone please ...

Establishing a primary data format for a backbone collection

Is there a way to configure a Backbone collection to always include a content type of "application/json" in every request it makes? I have tried the following code snippets: myCollection = Backbone.Collection.extend({ headers: {"Content-Type": 'ap ...

JavaScript allows users to create a single string by merging multiple strings positioned at various points

Let's say we have 3 strings: s1 = "ab", s2 = "cd", s3 = "ef". The task is to form a new string by merging s1, s2, and s3. The twist here is that the user has the freedom to choose the positions of these 3 strings. For example: s1 - position 3; s2 - ...

Unexpected token error occurs when making cross-domain AJAX requests to the server and receiving a JSON object

I have set up an express server to handle get requests to specific url endpoints. When responding to these requests, I am sending back data in JSON format to enable making Ajax calls and retrieving data from the page. To allow for cross-domain requests, I ...

Updating the DOM after making changes with Maquette involves calling the `renderMaquette

In a previous discussion, I expressed my desire to utilize Maquette as a foundational hyperscript language. Consequently, avoiding the use of maquette.projector is essential for me. However, despite successfully appending SVG objects created with Maquette ...