jquery how to get the pasted content

greenbandit picture greenbandit · Feb 29, 2012 · Viewed 40.6k times · Source

I have a little trouble catching a pasted text into my input:

 <input type="text" id="myid" val="default">
 $('#myid').on('paste',function(){
        console.log($('#myid').val());
 });  

console.log shows:

default

How I catch the pasted text and get ready to use?

Answer

cateyes picture cateyes · Jan 10, 2014

The accepted answer is actually hacky and ugly, seems to be suggested quite often for the paste event on stackoverflow. I think a better way to do it is this

$('#someInput').bind('paste', function(e) {
    var data = e.originalEvent.clipboardData.getData('Text');
    //IE9 Equivalent ==> window.clipboardData.getData("Text");   
});