Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paste event gets fired before text is in textbox

events: { "paste .youtube-url" : "addUrl" }

addUrl: function(){
  console.log(this.$(".youtube-url").val());

So lets say I paste "bad" into the textbox first time

console output: (an empty string)

then if I paste append something like "coder"

console output: bad

instead of whats inside the box "badcoder", I guess this i because the pseudo paste event is fired before the text is inserted.

like image 932
skyw00lker Avatar asked Mar 13 '12 18:03

skyw00lker


1 Answers

Instead of using the paste event you could use the keyup event which fires if someone pastes but also only fires after the value for the input has been updated.

UPDATE

Good comment from @Micah (and @JohnnyO). Here's a fix I found to work:

$('input').on('paste', function () {
    var that = this;
    setTimeout(function () {
        alert(that.value);
    }, 0);
});​

This sets a timeout so the code that reads the input's value is only run after the rest of the code in the stack has been run. I've only tested in Chrome 21 but the zero-time-timeout seems to do the trick.

Demo: http://jsfiddle.net/H4K4R/

like image 51
Jasper Avatar answered Nov 14 '22 22:11

Jasper