Trouble with VueJS: @change event not triggered when <input> value changes

In my VueJS project, I am working with two separate components:

One is a modal and the other is a form.

Within the modal component, the user inputs a PIN which is then confirmed. This value is then set to an input tag in the form component to save it.

To set the value in the modal component, I use this simple line of code:

document.getElementById('pin').value = this.pin_input;

The input tag within the form component looks like this:

<input type="hidden" @change="submit()" id="pin">

Although the value of this input tag is correctly being set in the console, the @change="submit()" event is not being triggered when the value changes.

The submit method within the form component that is not being called is as follows:

methods : {
   submit : function(){
       console.log("SUBMIT HERE");
   }    
}

What could be the reason behind my input tag's @change event not getting called?

Answer №1

When you set a value on a DOM element, the input and change events are not triggered automatically. As a result, any event listeners you have set up in Vue will not be activated.

To solve this issue, you can manually trigger these events so that Vue can recognize them.

const targetElement = document.getElementById('pin');
targetElement.value = this.pin_input;
// This method works effectively with most modern browsers.
const newEvent = new Event('change');
targetElement.dispatchEvent(newEvent);

Answer №2

In my opinion, when it comes to communicating between components, it is preferable to:

  1. implement some form of state management like Vuex from Vue.js (https://vuex.vuejs.org/) and store input values separately or

  2. explore using custom methods as described in the Vue.js documentation (https://v2.vuejs.org/v2/guide/components-custom-events.html)

  3. utilize a parent component to pass callbacks into child components.

It's important to note that "@change" may not be suitable for your specific scenario.

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

Inspect each element of the array individually, and issue a warning if any two elements are identical

Trying to set up an add to cart feature and feeling a bit lost. Unsure if I should be using cookies or local storage for this task, as I'm familiar with localstorage but not sure if it's the best approach. Working with vue.js using Vuex. Ideally, ...

Is searching for duplicate entries in an array using a specific key?

Below is an array structure: [ { "Date": "2020-07", "data": [ { "id": "35ebd073-600c-4be4-a750-41c4be5ed24a", "Date": "2020-07-03T00:00:00.000Z", ...

Optimal method for managing errors in Flask when handling AJAX requests from the front end

I'm currently working on a React application that communicates with a Python Flask server. One of the features I am adding allows users to change their passwords. To do this, an AJAX request is sent from React to Flask, containing both the old and ne ...

Getting the total number of child elements in a web page using Selenium web-driver with Node.js

I've been looking everywhere, but I can't seem to find the answer Here's my HTML code: <form id="search_form_homepage" > ... <div class="search__autocomplete" style="display: block;"> &l ...

What is the best way to transmit data via $router.push in Vue.js?

I am currently working on implementing an alert component for a CRUD application using Vue.js. My goal is to pass a message to another component once data has been successfully saved. I attempted to achieve this by passing the data through $router.push lik ...

Using Vue Formulate to effortlessly upload multiple images

One of my projects involves using a Vue Formulate component for uploading multiple images to an Express.js backend. Here is the code snippet for the Vue Formulate component: <FormulateInput type="image" name="property_ ...

Navigating Fragment GET parameters in Vue Router — Tips and Tricks

Having experimented with integrating Gitlab's OAuth using Web Application Flow in the backend, I am now exploring Implicit Grant in my Vue frontend. The access code provided by the API is in fragment URL parameter format, utilizing the hash symbol #f ...

Creating a nested list in AngularJS using the ng-repeat directive

My experience with AngularJS is limited, but I am tasked with modifying a web app that requires nesting lists inside each other: <ul class="first-level"> <li ng-repeat="item in list">{{item.parentNo1}} <ul class="level-2"> ...

Merging two divs in AngularJS

Currently in the process of converting an application from jQuery to AngularJS, I'm faced with a challenge involving combining two twin beds into a king bed using a bootstrap button toggle. Let's simplify this scenario by imagining two rounded bo ...

Is Nuxt's FingerprintJS Module the Ultimate Server and Client Solution?

I am currently using fingerprintJS in my NuxtJS+Firebase project VuexStore. When I call the function on the client side, I can retrieve the Visitor ID. However, I am encountering issues when trying to use it on the server side, such as in nuxtServerInit. ...

JQuery unable to retrieve the value from a radio button

Currently, I'm facing an issue with a form featuring image-based radio buttons. The problem arises when attempting to initiate an AJAX call based on the chosen value (either 10, 30, or 100). Despite selecting a value other than 10, it keeps defaulting ...

The challenge with Normalizr library in Redux and how to address it

I am currently attempting to utilize the Normalizr library by Paul Armstrong in order to flatten the Redux state. Below are the schema definitions: import { normalize, schema } from 'normalizr' const fooSchema = new schema.Entity('foos&apo ...

Is optgroup malfunctioning in IE 10?

My main question is: does optgroup not function properly in IE? The Plnkr code works fine in Chrome but encounters issues in IE 10. I'm trying to find a solution that will work across both browsers. Is this a known problem? In Chrome, I can expand/co ...

Can you explain the contrast between window.performance and PerformanceObserver?

As I delve into exploring the performance APIs, I have come across window.performance and PerformanceObserver. These two functionalities seem to serve similar purposes. For instance, if I need to obtain the FCP time, I can retrieve it from performance.getE ...

Transfer JSON information to another server (PayU) utilizing express and angular for seamless communication

I need some guidance on how to send JSON data to the PayU server for a payment application I'm developing. Can anyone help me with this? The information I need to pass needs to be sent as a POST request to https://secure.snd.payu.com/api/v2_1/orders ...

Getting the card height in Bootstrap Vue: Tips and tricks

Utilizing the card component in bootstrap-vue is my current project. One challenge I'm facing is determining the height of the card post-rendering, as the height varies depending on the amount of text within. Can you provide a possible solution? ...

The computed properties in Vue are not receiving any values when an array is passed using v-bind

Embarking on my Vue journey with a simple example, I am attempting to calculate the sum of items based on the provided data. If you wish to explore the complete example, it can be accessed in this jsffile. Here is the component structure: Vue.component(&a ...

Is my understanding of HTML and JavaScript code accurate?

Explaining lines of code to my class for homework has been a challenge. I tried my best to break it down, but I'm not entirely confident in my accuracy. My grade depends on being 100% precise and detailed. <!DOCTYPE html> The first command dec ...

Get the large data file in sections

I ran a test script that looks like this: async function testDownload() { try{ var urls = ['https://localhost:54373/analyzer/test1','https://localhost:54373/analyzer/test2'] var fullFile = new Blob(); for (le ...

"Lost in the mist of the unfulfilled promise

As a newcomer to JavaScript, I recently came across a text document filled with nouns and thought it would be a great idea to create an API using these words. I parsed the file and stored the nouns in a List: public List<Noun> getData() throws IOEx ...