Return function value executed by Keyup in the VUE source field

Asked

Viewed 53 times

0

How do I get the return value of this function into the field where you are running Keyup?

<input type="text" v-model="valor_calc_dinheiro" v-on:keyup.stop.prevent="mascara_dinheiro($event.target.value)"           ref="valor_calc_dinheiro">

mascara_dinheiro : function(valor){
v=valor.replace(/(\d{1})(\d{1,2})$/,"$1.$2")
alert(v);
},
  • this.valor_calc_dinheiro = valor.replace(/(\d{1})(\d{1,2})$/,"$1.$2")

1 answer

0

Just pass the event and so you have available the event.target for example...

That is to say:

<input type="text" v-model="valor_calc_dinheiro" v-on:keyup.stop.prevent="mascara_dinheiro($event)"           ref="valor_calc_dinheiro">

And then in the method:

mascara_dinheiro: function(e){
    const v = e.target.value.replace(/(\d{1})(\d{1,2})$/,"$1.$2")
    alert(v);
    e.target.value = v;
},

Note: note that I put const v and not only v. You should avoid global variables, it is always important to declare variables before using them. You can read more about it here: var, const or Let? Which to use?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.