Button disabled attribute is added before toggling the input field upon clicking

I am having an issue with the Add button on my webpage. When clicked, this button should reveal an input field for the user to use. However, I want the Add button to be disabled once it has been clicked and the input field appears. Right now, when I try to disable the Add button, it seems to happen before the input field is displayed.

This is the code I am currently using:

button.btn(data-toggle='collapse', data-target='#addNewField', @click='disableBtn($event)') Add

#addNewField.collapse
    input.form-control(type='text', v-model='inputValue'...)

disableBtn(event: Event) {
   let el = event.target as HTMLElement;
   el.querySelector("button");
   el.setAttribute("disabled", "");
}

How can I ensure that the button is disabled only after the input field becomes visible?

Answer №1

Vue is a great tool for managing state, so it's recommended to use state for everything unless you have specific knowledge on when not to.

If you are wondering how to achieve a certain functionality, you can implement something similar to the following code snippet:

<template>
  <div>
    <button @click="add" :disabled="isAdding">
      Add
    </button>

    <input v-show="isAdding" v-model="inputValue" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      isAdding: false,
      inputValue: "",
    }
  },

  methods: {
    add() {
      this.isAdding = true
    }
  }
}
</script>

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

Search for styling cues within the text and transform them into distinct elements

I was inspired to develop a unique text editor that utilizes cues within the text, such as :strong:, to set formatting rules. Here is the code snippet I have so far: <?php $document = $_GET["document"]; $user = $_GET["user"]; if ($user != n ...

Make specific attributes as non-compulsory in Typescript

Seeking a solution that can achieve the following: type MakeOptional<T, U> = /* ... */; interface A { foo: string; bar: number; baz: Date; } type AWithOptionalFoo = MakeOptional<A, 'foo'>; // desired output: { foo?: string | ...

Jest is having trouble locating the module that ends with ".svg?react" when using Vite in combination with React, TypeScript, and Jest

Currently, I am facing an issue while testing my app built using vite + react + ts. Jest is highlighting an error stating that it cannot locate the "svg?react" module when trying to create my dashboard component. The problem arises with Jest as soon as th ...

Are there any comparable options for NPM similar to Java's .war files?

When it comes to building and publishing various versions using Gradle and Java, I have found it easy to reroll by deploying a prior version if there are deployment issues. However, I can't seem to find a comparable process for NPM applications such ...

Exploring the power of using refs in Vue 3 with TypeScript and the Options API

Exploring the optimal way to utilize refs in conjunction with TypeScript and the Options API is my current focus. The documentation demonstrates it as shown below, but without TS. With TypeScript, however, they only cover its usage with Composition API. E ...

A guide to accessing parameters and defining types for customizable React and React-Native components

Recently, I developed a unique React Native Input Component that features a dark bottom border when focused. However, I've encountered an issue where VS Code Intellisense is not providing prop suggestions while using this component. function CustomInp ...

Utilizing Vue.js to dynamically render images within a TypeScript array

Currently, I am utilizing Vue 2 together with Typescript and have defined a type in my types.ts file: export type SetupSliderType = { name: SetupSliderEnum; image: HTMLImageElement; title: keyof I18nMessages; component: Function; } No errors are d ...

Swapping out .generateFile() within jQuery

Is there a way to replace document.createElement in jQuery? for (let word in myDictionary) { let buttonElement = document.createElement('button'); $(buttonElement).html(word); $(buttonElement).on("click", createOnClickListener(myDict ...

Populate Highchart Series with JSON

I am attempting to create a column chart using JSON for the xAxis categories and series, but unfortunately, no column chart is being generated as depicted in the image below. Here is the structure of my JSON (already validated using an online JSON validat ...

What causes the typescript error in my code: The argument being passed is either a string, an array of FilterData, an array of numbers, or an array of Moments, which is not compatible with a parameter of type 'string'?

When writing my code, I have the need to utilize various types for different scenarios. Depending on the situation, the type may be a string, number[], FilterData[] (a custom type), or Moment[]. To address this requirement, I defined the type as: export ty ...

Click and release file upload

I am working on creating a drag and drop upload feature where the user can drop files onto a div, and then click an upload button to send them to the server. I'm facing an issue with JavaScript not recognizing the variable fd. How can I pass that vari ...

Updating an array in Vue.js without the need to reload all the data

Recently delving into Vue.js and JavaScript, I'm seeking guidance on updating an array (on Add/Edit/Delete) without having to reload all the data. The goal is to load all data only when initially opening the page with Addresses. Take a look at my cod ...

Is there a way to dynamically exclude files from the TypeScript compiler?

Currently, I am in the process of setting up a node/typescript server for a real-time application. Both my server and client are located within the same folder. My goal is to exclude "src/client" from the typescript compiler when executing the "server:dev ...

Struggling to properly line up the baselines of navigation list items that are styled as circular elements using CSS

I have transformed my navigation menu into a series of CSS circles with text inside. The issue I am facing is that the text spills out unevenly based on the amount of content in each circle. To address this, I used a JavaScript solution to center-align the ...

What could be preventing me from accessing global variables when using $.post in jQuery and returning a function?

let strTest = ''; $.post( 'test.php', { post_test : 1 }, function(data) { strTest = data.split('a'); $.post( 'test_2.php', { post_test_2 : 2 }, func ...

Encountering a JSDOM error of "ECONNREFUSED 127.0.0.1:80" during test execution on Azure DevOps

While running the job on Azure DevOps with yarn test:unit, I encounter a repeating error multiple times. However, this error does not seem to affect the passing of tests. The project utilizes vue and jest for testing purposes. Interestingly, when running t ...

Do you need to have typing or definition files for each JavaScript library you utilize in TypeScript?

Do you need typings for every JavaScript library used in TypeScript? If not, how can you eliminate errors and utilize a library that doesn't have available definition files? import { NotificationContainer, NotificationManager } from 'react-notif ...

Specify the type of nested object exclusively with string values

I am working on creating a complex nested object structure with string values only, as shown below: let obj = { foo: "bar", foo2: "bar2", nestedFoo: { foo3: "bar3", anotherNested: { foo4: "bar4" ...

The 401 error code is returned when making a .NET Ajax

I am facing an issue with a WebMethod that retrieves schedule hours from a webservice. When I call it using Jquery Ajax, I encounter a 401 (Unauthorized) error. responseText: "{"Message":"Failed to Authenticate","StackTrace":null,"ExceptionType":"System.I ...

Strategies for navigating a table to collect individual element values

Below is an example of an HTML table: <table border="1"> <tr> <th>Map</th> <th>Source Table</th> <th>Target Table</th> </tr> <tr class="schema"> <td> <input ...