Is it possible to use v-model on an input that is being dynamically updated by another script?

How can I retrieve the lat and lng values stored in two input fields when a user changes the marker's position on a Google map? When the user clicks to select a new place, I need to update these inputs accordingly. I attempted using v-model but it only triggers when I manually type or paste into the inputs. Is there a method to capture these input values (similar to on-change) within my Vue instance?

Answer №1

If you want Vue.js to recognize changes in your input's value triggered by an external script, you need to use the following code:

document.querySelector('#element-id').dispatchEvent(new Event('input'))

This method is effective for both <input> and <textarea> elements. However, for <select> elements, you will need to call:

document.querySelector('#element-id').dispatchEvent(new Event('change'))

Answer №2

Monitoring your two variables is crucial, and make sure to properly assign the value to the new variable.

Answer №3

Monitor your variables using the watch properties:

Example with HTML:

<div id="element">
  <form>
    <input v-model="itemA">
    <input v-model="itemB">
  </form>
  <p>{{ itemA }}</p>
  <p>{{ itemB }}</p>
</div> 

Include JavaScript as well:

new Vue({
   el: "#element",
   data: {
       itemA: '',
       itemB: ''
   },
   watch: {
      itemA: function(value) {
         this.itemA = value
       },
      itemB: function(value) {
         this.itemB = value
      }
   }
});

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

What is the best way to store client-uploaded files on the client-side using Bootstrap forms and Express server code?

Essentially, the user submits a file for upload, which is then saved on the client-side (I believe this is handled by PHP), and the upload form I am utilizing is a Bootstrap HTML form. On the server side, I am writing my code with Express. I'm feeling ...

The basic function is ineffective when used within an if-condition

I am currently dealing with a JSON file that has some nesting: { "name": "1370", "children": [ { "name": "Position X", "value": -1 }, {...} ] "matches": [ { "certainty": 100, "match": { "name": "1370 ...

Customizing the color of cells in an HTML table within a JSON based on their status

Would you like to learn how to customize the color of cells in an HTML table based on the status of a college semester's course map? The table represents all the necessary courses for your major, with green indicating completed courses, yellow for cou ...

Showing Variables in JavaScript

<!DOCTYPE HTML> <html> <body> <button onclick="question++">Increment</button> <script> function test() { var question = 0; } </script> </body> </html> Qu ...

Is the sudden disconnection from Chrome after a WebSocket handshake related to a domain mismatch or is it possibly a bug in Chrome?

I created my own WebSocket server using Python, but I encountered an issue where Chrome 4.0.249.78 dev (36714) always disconnects after the handshake process. Wanting to rule out any issues with my code, I tested it using the WebSocket server from , only t ...

The Vue production build displays a blank page despite all assets being successfully loaded

After running npm run build, I noticed that my vue production build was displaying a blank page with the styled background color from my CSS applied. Looking at the page source, I saw that the JS code was loading correctly but the content inside my app d ...

Receiving JSON in a C# web service: A step-by-step guide

I created a JSON string using jQuery and now I want to send it to a C# web API controller. Here is an example of the JSON object: {"Name":"","Type":"4","Meals":["2","3"],"Excludes":["Beef","Chicken"]} I attempted to send it with a URL structure like thi ...

Issue with Mjpg paparazzo.js functionality not functioning as expected

I am currently exploring alternative methods to view my security cameras without relying on browser support for mjpg streaming. I have come across Paparazzo.js, which appears to be a promising solution that I want to experiment with: https://github.com/rod ...

Using scriptlet based IDs in jQuery selectors involves incorporating JavaScript syntax within the jQuery selector to

I need to incorporate dynamic ids in my form, which are based on jsp variables within a scriptlet. How do I correctly select the desired element using jQuery's id selector without encountering any errors? Below is the code snippet: <form name="in ...

Is it possible to assign a different array to a variable in JavaScript?

I'm facing an issue with manipulating arrays in JavaScript within a function. This problem arises from an exercise found in the book Eloquent JavaScript, focusing on two specific functions: reverseArray(): designed to produce a new array that is the ...

Names picked at random and presented in the innerHTML

I am currently working on a project that involves displaying random names from an array in a text area named "textbox". Currently, I am using math.random() to pick a name randomly, but it only generates a new random name when the page is reloaded. How ca ...

Receiving a Javascript Promise from a $.ajax request

Trying to convert a $.ajax() statement into an es6 Promise and return it as an es6 promise. The goal is to have an application layer with Create, Update, Delete calls to the Microsoft Dynamics Web API that return an es6 Promise for reuse across multiple pa ...

Error in PHP script when making an AJAX call

First of all, I want to thank you all for your help. The script is creating a table but sending empty information. I have tried to modify it like this: However, when I call the script, it gives me an error: So, I have edited the script code to clean it ...

Issues with image loading in Next JS and Cloudinary

I'm currently delving into the world of Next JS and attempting to convert my app into a static site. In doing so, I've opted to utilize Cloudinary for image processing; however, I'm encountering issues with the images not displaying. Next JS ...

Struggling to reflect changes in the database using the updated value in the localStorage

I have a table where the td is set to contenteditable. To update the value of the td in my database, I decided to use localStorage. When the save button is clicked, the inputted value in the td will be saved to localStorage and retrieved via AJAX to replac ...

Idiosyncratic TypeScript behavior: Extending a class with comparable namespace structure

Lately, I've been working on creating a type library for a JavaScript written library. As I was defining all the namespaces, classes, and interfaces, I encountered an error TS2417 with some of the classes. I double-checked for any issues with method o ...

How can I programmatically refresh a recursive ng-include in AngularJS?

Using a recursive script within my Angular application, I am displaying a visualization of multiple objects. However, whenever I modify the structure of an object dynamically, the view does not automatically update. It appears that the ng-include directiv ...

Step-by-step Guide to Setting pageProps Property in Next.js Pages

When looking at the code snippet provided in .../pages/_app.js, the Component refers to the component that is being exported from the current page. For instance, if you were to visit , the exported component in .../pages/about.js would be assigned as the ...

Is there a way to ensure that my Delete Entry Button is fully operational?

Excuse my lack of experience in coding. I've been struggling with getting my delete button to function properly on both my webpage and my MySQL database. Right now, when I click the delete button, it just redirects me to a URL ending in /delete withou ...

What is the process for altering the active status in pagination?

How can I make the active class in my pagination appear in red? Here's the code snippet I have: In my script.js file: $(".pagination").append("<li class='page-item' id='previous-page'><a class='page-li ...