jQuery bind to Paste Event, how to get the content of the paste

AnApprentice picture AnApprentice · Jul 23, 2012 · Viewed 157.7k times · Source

I have a jquery token tagit plugin and I want to bind to the paste event to add items correctly.

I'm able to bind to the paste event like so:

    .bind("paste", paste_input)

...

function paste_input(e) {
    console.log(e)
    return false;
}

How can I obtain the actual pasted content value?

Answer

jeff picture jeff · Jul 23, 2012

There is an onpaste event that works in modern day browsers. You can access the pasted data using the getData function on the clipboardData object.

$("#textareaid").bind("paste", function(e){
    // access the clipboard using the api
    var pastedData = e.originalEvent.clipboardData.getData('text');
    alert(pastedData);
} );

Note that bind and unbind are deprecated as of jQuery 3. The preferred call is to on.

All modern day browsers support the Clipboard API.

See also: In Jquery How to handle paste?