Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change v-model value from JS

I'm using Google autocomplete address form. I found example at google official web page. Everything is fine. Everything works! but it's native Javascript,

I have Vue application and I don't like how I change text input values from JS script. The idea is that when I change something in main input, JS event listener should change values for other inputs:

 document.getElementById(addressType).value = val;

Problem is that I should use "document" to change values:

 document.getElementById('street_number').value

I would like to have something like tat:

 <input type="text" v-model="input.address" ref="addressRef">

And to read values:

export default {
        data() {
            return {
                input: {
                    address: "",
                    ...
                }

            };
        },
        methods: {
            test() {
                console.log(this.input.address);
                console.log(this.$refs.addressRef);
        }
       }

So the question is:

  • How to set the value from JS code to update binding values? Right now values are null because I use "getElementById("id").value = val"
like image 352
grep Avatar asked May 28 '19 18:05

grep


People also ask

What is v-model in JavaScript?

The v-model is a two-way binding which means if you change the input value, the bound data will be changed. The v-model directive is used to create two-way data bindings on form input, textarea, and select elements. Example: HTML.

What is @input in VUE JS?

It provides two-way data binding by binding the input text element and the value binded to a variable assigned.

What is the difference between value and v-model?

v-model is for two way bindings means: if you change input value, the bound data will be changed and vice versa. But v-bind:value is called one way binding that means: you can change input value by changing bound data but you can't change bound data by changing input value through the element.

How do I get my Vue form value?

To get an input value in Vue, we can use the v-model directive to set up a two-way binding between the value and a variable. Every time the user changes the text in the input field, the text variable will be updated automatically. We can then use this variable to perform an action or display information.


1 Answers

You can emit input event afterwards which v-model relies on for updating its value:

let el = document.getElementById("id");
el.value = val;
el.dispatchEvent(new Event('input'));

In action:

Vue.config.devtools = false

const app = new Vue({
  el: '#app',
  
  data: {
    message: null
  },
  
  methods: {
    updateBinding() {
      let el = document.getElementById("input");
      el.value = 'Hello!';    
      el.dispatchEvent(new Event('input'));
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button @click="updateBinding">Click me </button><br>
  <input id="input" v-model="message" placeholder="edit me">
  <p>Message is: {{ message }}</p>
</div>
like image 143
Orkhan Alikhanov Avatar answered Oct 08 '22 10:10

Orkhan Alikhanov