Modify the disabled attribute in an input element with a button click in Vue.js 2.x

With a loop generating multiple rows, each containing its own 'input' with description and action buttons, including an edit button that toggles the 'disabled' attribute. I'm struggling to figure out how to achieve this. I believe I need to use $emit.

<template>
  <section id="tasks-list">
    <ul>
      <task
        v-for="(item, index) in filteredTasksList"
        v-bind:key="item.id"
        v-bind:index="index"
        v-bind:item="item"
        v-bind:tasks="tasks"
      />
    </ul>    
  </section>
</template>

and task component:

<template>
    <li :class="{checked: item.status}" class="task">
      <div class="task-description">
        <span>{{item.description}}</span>
        <input type="text" v-model="item.description" :disabled="true">
      </div>
      <div class="task-actions">
        <button class="btn-edit" v-on:click="disableItem()">{{translation.edit}}</button>
      </div>
    </li>
</template>

<script>
  export default {
    name: 'task',
    props: {
      item: { required: true },
      index: { reuqired: true },
      tasks: { required: true },
      search: { require: true }
    },
    methods: {
      disableItem(event){
        //part responsible for changing disabled attr
      }
    }
  }
</script>

Answer №1

If the disabled state and its toggle handler are both contained within the <task> component, it is best to keep the logic confined to that component. There is no requirement to trigger an event using $emit to communicate with a parent component, unless there is a need to manage state at a higher level of the component hierarchy.

Within the <task> component, you can achieve this by utilizing a boolean variable and updating its value upon button click through the disableItem handler.

The input element should be updated as follows:

<input type="text" v-model="item.description" :disabled="isDisabled">

In addition, create a new variable in the component's data object and modify the disableItem method accordingly:

data () {
  return {
    isDisabled: false
  }
},
disableItem () {
  this.isDisabled = true
}

Furthermore, it is not mandatory to call the disableItem method directly within the click handler; instead, simply reference it with v-on:click="disableItem".

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

Callback function triggered upon the creation of a DOM node

I am in search of a way to have a specific callback function run each time a div that matches a particular selector is added to the DOM. I have explored the DOM events documentation and the event closest to what I need seems to be "load", however, it does ...

Unable to retrieve file on Linux system through PHP

<?php // Ensuring that an ID is provided include('include/function.php'); if(isset($_GET['id'])) { // Retrieving the ID $id = intval($_GET['id']); // Validating the ID if($id <= 0) { die('Th ...

How can you use ng-click to re-sort data that has already been loaded using Angular's ng-click?

I'm having trouble switching between loading and sorting the information in the table using ng-click. The functions to load and sort work correctly individually, but I can't seem to switch between the two. It seems like I reset the countries data ...

A guide to incorporating suspense and dynamic components into Vue 3

I have encountered an issue with using suspense in my code. I am importing four different components asynchronously and when I switch between them by clicking buttons, the loading slots in suspense only show for the first time. It doesn't work when I ...

The request header fails to function properly when used for cross-domain Ajax requests

I'm facing a challenge with adding a parameter in the request header. It works smoothly for calls within the same domain, but when making a call to a different domain (the API), I need to adjust the header parameter itself. Here is the snippet of cod ...

Actuate the Puppeteer by either pressing the Enter key or clicking on the OK button within

Clicking OK button on dialog Having reached the end of my Puppeteer script, I am now faced with the task of clicking the OK button on a confirmation dialog box (refer to image linked above) or simply pressing the enter key. Despite attempting various solu ...

Angular unable to register service worker

Looking to implement push notifications in my Angular app using vanilla JavaScript instead of the Angular service worker or @angular/pwa. In angular.json, I've specified the path to the js file under the script option. However, when the service worke ...

Is it possible to pass multiple functions to the parent component in ReactJs if using OnChange() as a prop?

Just getting started with React and trying to pass data from a child component to a parent component. <div className="filter-module"> <i className="fas fa-sign-in-alt icon"></i> < ...

using vue-router to pass an array of parameters with individual keys

Is there a way to generate a vue-router link with an array containing string keys as a query parameter? I need the resulting URL to look like url?param[key]=value It is crucial that these types of query parameters match our existing backend infrastructur ...

Tips for modifiying date format in React app

I'm encountering an issue where I can't modify the date format, preventing me from displaying the date on the frontend. Currently, I'm utilizing the dateformat package. import dateFormat from "dateformat"; const EditFinancialInfo ...

Using the tab key in Chrome or IE11 doesn't advance to the next field on forms

When tabbing through the form, I noticed that the password field is causing an issue. In Firefox, everything works fine, but in Chrome and IE11, the tab doesn't move forward or backward when reaching the password field. <tr> <td style ...

When conditions are met, all items are added to the array

In my Angular app, I have a user list that I am trying to filter based on the condition age > 45. However, all users are being pushed into the validForScheme array instead of just those meeting the condition. I am unable to figure out what is going wron ...

Command for Sniping with Discord.js

I am currently working on creating a snipe command using Discord.js in my bot. I have set up command handlers and everything seems to be working fine, including the on messageDelete event. However, I encounter an error when I delete a user message and try ...

Enter your own text into the input fields to create a personalized message

Is there a way to eliminate duplicates when checking the checkboxes? <label><input type="checkbox" data-bValue="100" data-sValue="sV1" data-nValue="nV1" name="layers"> 100</label><label><input type="checkbox" data-bValue="200" d ...

Loop through a non-array or non-object / handling both arrays and non-arrays equally

Sometimes, I find myself needing to handle arrays and single objects in a similar manner. For instance, I may have an object property that can be either an array or just a string (like the scale property): [ { "name": "Experiment type14", "id": ...

Encountering an issue with undefined this.auth.settings when implementing Firebase Phone Authentication in NextJS 14

I am facing an issue with authenticating users using the firebase OTP system. Can someone assist me with resolving the problem of 'this.auth.settings is undefined' while implementing Firebase Phone Authentication in NextJS 14? The code snippet ...

The Mobile Side Menu Is Not Scrollable

I've encountered an issue with the side menu on my website. While it works perfectly fine on PC, it refuses to scroll on mobile devices. I've tested it on both my iPhone 5 and iPad, but no luck. Additionally, the toggle button isn't function ...

Where do JQuery and framesets vanish to?

Whenever I attempt to use the console to create an element with the tag frameset, it returns no result: $('<div id="content" data-something="hello" />') => [<div id=​"content" data-something=​"hello">​</div>​] $(&apo ...

Creating a variable before defining a function

Is there a way to define 'sender_psid' globally instead of inside the 'app.post' function so that it can be accessed by the function calls at the bottom? Note: Declaring it outside the function won't work due to 'webhook.even ...

Executing JavaScript functions with Python and BeautifulSoup

My current project involves web scraping to extract data from a website. While I can successfully retrieve information present in the DOM, I am facing a challenge with data that is rendered through a JavaScript onClick function. One potential solution is ...