In a Vue app I have a paste listener on a textarea with the intention to run validation code when the user pastes data into this field. When I log the paste event I can see in the console that the data that was pasted into the field is there under event -> target -> value. I can't seem to access this with event.target.value though. What am I doing wrong?
Minimal example:
<div id="app">
<textarea name="myField" @paste="onPaste"></textarea>
<p>Field name: {{ fieldName }}</p>
<p>Pasted data: {{ pasted }}</p>
</div>
var app = new Vue({
el: '#app',
data: {
fieldName: '',
pasted: ''
},
methods: {
onPaste(event){
console.log(event)
this.message = event.target.name
this.paste = event.target.value
}
}
})
First of all, your jsfiddle has a minor typo (
this.pasteinstead ofthis.pasted).Secondly, you need to use the "getData" method from the clipboardData property to access the text.
https://developer.mozilla.org/en-US/docs/Web/API/Element/paste_event
https://jsfiddle.net/zfuwy9ep/
That said, if you want to get the whole string inside the text area after something was pasted, you could wait until the current items in the event queue have been resolved, and read the value of the textarea afterwards
https://jsfiddle.net/cjq1bw5u/