I'm trying to send data to the server via Backbone
if the users writes or paste a string inside an input text element.
in the Backbone events I thought something like this but it doesn't work:
events:{
"click .close":"closeResults",
"keypress input":"fetchData",
"paste input":"fetchData"
},
fetchData:function (e) {
var $this = this;
window.setTimeout(function () {
if ($.trim(e.target.value).length >= 3) {
console.log(e.target.value);
$this.collection.fetch({data: {limit: 10, term:$.trim(e.target.value)}});
}
}, 0);
}
If you switch to using the keyup
event instead of keypress
and paste
, it will work for pasting via the keyboard ( ⌘ + v
or Ctrl + v
) and typing normally.
If you use the input
event, it will work even if you right click and paste (in addition to the same expected behavior as keyup
).
More info on input
:
https://developer.mozilla.org/en-US/docs/Web/API/window.oninput
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With