Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass input text using v-on:change to my vue method?

How can I pass the input value from my html to my vue method called checkEx ist() ? I would like to retrieve that value within my checkExist() method. Could any advice how I can do this? I am still new to vue.

HTML:

<input type="text" class="form-control" placeholder="Email*" v-model="form.email" v-validate="'required|email'"  v-on:change="checkExist">

VUE ELEMENT:

Vue.component('button-counter', {
  data: function () {
    return {
      count: 0
    }
  },
  methods: {
       checkExist:function(){
       }
  }
})
like image 654
ApplePie Avatar asked Aug 21 '18 16:08

ApplePie


People also ask

How do I get input value from Vue?

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.

What does V-bind do in Vue?

The v-bind directive is a Vuejs directive used to bind one or more attributes, or a component prop to an element. If that attribute is binded to our data defined in Vuejs instance then dynamically changes can be observed as data changes.

What is V text in VUE JS?

The v-text directive is a Vue. js directive used to update a element's textContent with our data. It is just a nice alternative to Mustache syntax. First, we will create a div element with id as app and let's apply the v-text directive to this element with data as a message.

What is V-bind value?

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. v-model is intended to be used with form elements.


1 Answers

First you need to define form:{email:"", ...} in the data as well.
Pass $event inside checkExist() .
Something like this,

function callMe() {
  var vm = new Vue({
    el: '#root',
    data: {
     form:{email:""},
     email:""
    },
    methods: {
       checkExist(event){

         this.email=event.target.value;

       } 
    
    }

  })
}
callMe();
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div id='root'>
 
 <input type="text" @input="checkExist($event)" class="form-control" placeholder="Email*" v-model="form.email">
<p>email:  {{email}}</p>
</div>
like image 173
Helping hand Avatar answered Oct 03 '22 01:10

Helping hand