I’m building a component in Vue.js. I have an input on the page where the user can request a certain credit amount. Currently, I’m trying to make a function that will log the input amount to the console, as I type it in. (Eventually, I’m going to show/hide the requested documents based on the user input. I don’t want them to have to click a submit button.)
The code below logs it when I tab/click out of the input field. Here’s my component.vue:
JavaScript
x
33
33
1
<template>
2
<div class="col s12 m4">
3
<div class="card large">
4
<div class="card-content">
5
<span class="card-title">Credit Limit Request</span>
6
<form action="">
7
<div class="input-field">
8
<input v-model="creditLimit" v-on:change="logCreditLimit" id="credit-limit-input" type="text">
9
<label for="credit-limit-input">Credit Limit Amount</label>
10
</div>
11
</form>
12
<p>1. If requesting $50,000 or more, please attach Current Balance Sheet (less than 1 yr old).</p>
13
<p>2. If requesting $250,000 or more, also attach Current Income Statement and Latest Income Tax Return.</p>
14
</div>
15
</div>
16
</div>
17
</div>
18
</template>
19
20
<script>
21
export default {
22
name: 'licenserow',
23
data: () => ({
24
creditLimit: ""
25
}),
26
methods: {
27
logCreditLimit: function (){
28
console.log(this.creditLimit)
29
}
30
}
31
}
32
</script>
33
If I change methods
to computed
, it works – but I get an error saying Invalid handler for event: change
every time it logs the value.
Advertisement
Answer
Use the input
event.
JavaScript
1
2
1
<input v-model="creditLimit" v-on:input="logCreditLimit" id="credit-limit-input" type="text">
2
change
only fires when the element loses focus for input elements. input
fires each time the text changes.