9

I'm trying to get the input value on keyup using Vuejs 2 but always get an empty result like the data. Please see code below.

HTML:

<input type="email" @keyup="this.$data.email= $event.target.value" class="form-control" id="email" placeholder="Username (your work email)">
<button type="button" class="btn btn-primary btn-block inactive" @click="submit">Log in</button>

Script:

data () {
  return {
    email: '',
  }
},
methods: {
  submit () {
    alert(this.$data.email)
  }
}

2 Answers 2

13

You need to reference email (not this.$data.email):

<input type="email" @keyup="email = $event.target.value" class="form-control" id="email" placeholder="Username (your work email)">

But also, why not use v-model to bind email to the input?

new Vue({
  el: '#app',
  data () {
    return {
      email: '',
    }
  },
  methods: {
    submit () {
      alert(this.email)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="app">
  <input type="email" v-model="email" class="form-control" id="email" placeholder="Username (your work email)">
  <button type="button" class="btn btn-primary btn-block inactive" @click="submit">Log in</button>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0
@keyup.enter.native="submitMETHOD"

1 Comment

Please include a brief description of your suggestions.

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.