Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle an input text "paste" event with Backbone

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);
}
like image 335
vitto Avatar asked Mar 24 '23 00:03

vitto


1 Answers

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

like image 188
burin Avatar answered Apr 17 '23 19:04

burin