1

I have a application where I'm updating value of input element on button click.

I'm using Vue JS v-model for data binding.

HTML :

<div id="app">
<div>{{ response }}</div> <br>
 <input type="text" v-model="response.ruleName" value="" placeholder="" class="editing">
 <button @click="myalert">Click me</button>
</div>

JS :

var response = {
    ruleName: "ruleJohn",
}
new Vue({
  el: "#app",
  data () {
        return {
            response
        }
  },
  methods: {
    myalert: function(e) {
      document.querySelector('.editing').value = e.target.innerText;
        },

  }
})

Here is working fiddle : https://jsfiddle.net/38Lj971g/

Now, When I try to edit input value with keyboard, response values does change.

When I try to click button, Though input value changes but response doesn't.

How to deal with v-model for such on-click data updates ?

1 Answer 1

2

Don't edit the input value directly, instead edit the v-model it gets its data from, like so:

<button @click="response.ruleName = 'Click me'">Click me</button>

or, if you wish to use the method:

myalert: function(e) {
  this.response.ruleName = e.target.innerText;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.