Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get value of text field and pass it to a method in Vue.js?

I am new to Vue and Vuetify, but that’s what I got so far after reading and trying:

I have the following template:

<template>
  <div>
    <div>
      <v-row>
        <v-text-field
          v-model="myText"
          label="enter a text (optional)"
        ></v-text-field>
      </v-row>
    </div>
    <v-btn color="#aaa" class="ml-4" outlined @click="getTest()">
      Check Value!
    </v-btn>
  </div>
</template>

and I have the "MyText" in the data as follow:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data() {
  return {
    myText: ''
  }

Now I want to check the value in the text field that the user has entered, but I am always getting it as empty string although I added it to the v-model.

here is where i am calling it:

methods: {
  getTest() {
    console.log(this.myText)
  }
}

So how can I read the value that users entered? and make sure it is the latest value that was entered.

>Solution :

Not sure what is the issue but this totally works

<template>
  <div>
    <div>
      <v-row>
        <v-text-field
          v-model="myText"
          label="enter a text (optional)"
        ></v-text-field>
      </v-row>
    </div>
    <v-btn color="#aaa" class="ml-4" outlined @click="getTest">
      Check Value!
    </v-btn>
    <p>value: {{ myText }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      myText: ''
    }
  },
  methods: {
    getTest() {
      console.log(this.myText)
    }
  }
}
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading