5

I can't get the html field that fires the event in its event handler (in javascript is the event.target).

I've a form with:

  • an input element attached to a function on change event
  • a function that manages the change event

My code is like the following:

var Main = {
    methods: {
      change(par) {
        console.log("The value is: " + par);
        //HERE I can't get "event.target"
      }
    }
};
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@1.4.0-beta.1/lib/index.js"></script>
<div id="app">
  <template>
    <el-input @change="change" placeholder="Input something here"/>
  </template>
</div>

I know I can call different functions, and in each one I know who is the relative input, but I'd like to use a common one.

Following the stacktrace I saw in element-ui.common.js the code of event fire:

handleChange: function handleChange(event) {
  this.$emit('change', event.target.value);
},

And there element passes just the value to the event handler.
Anyway, any idea to get the event.target, please?

Thanks

Alessandro
  • 4,382
  • 8
  • 36
  • 70

2 Answers2

4

You can use @input.native

var Main = {
    methods: {
      change(event) {
        console.log("The value is: " + event.target.value);
        //HERE I can't get "event.target"
      }
    }
};
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@1.4.0-beta.1/lib/index.js"></script>
<div id="app">
  <template>
    <el-input @input.native="change" placeholder="Input something here"/>
  </template>
</div>
Void Ray
  • 9,849
  • 4
  • 33
  • 53
  • 1
    This doesn't work if there are `:value` attribute in the field. My `el-input` got a `:value` attribute, no matter what I type, the returned event always show the original value, not the new one (Ex: current value is "grea", then I type "t", and the `event.target.value` is "grea", instead of "great") – Lê Quang Bảo Mar 06 '19 at 04:34
1

It looks like Element UI just eats the event and returns only the value. What you could do instead of @change(change) is @keyup.native(change) and then your par parameter will be the event rather than just the value.

Pat
  • 2,540
  • 1
  • 21
  • 28