Paste event in JavaScript

dom-events, javascript

Solution

The onpaste event should work in all modern browsers (UPD Including Opera >= 12.101).

Bind it in jQuery like this:

$('#txt').on('paste', function() {console.log('text pasted!')})​

Here's a live example: http://jsfiddle.net/7N6Xq/

In pure JavaScript it would look something like this for modern browsers

elem.addEventListener ("paste", handler, false);  // all browsers and IE9+

and for old IE versions:

elem.attachEvent ("onpaste", handler);  // IE<9

You can also combine it with oninput and other events (change, propertychange, dragdrop, etc.) to create a relatively bulletproof tracking of content change.

Footnotes:

1 Opera supports Clipboard API starting from Presto/2.10.286 which corresponds to 12.10 as suggested here. Blink versions of Opera (starting from 15) should also support it but I am unable to test it as there is still no Linux version.

Problem

How can I handle the paste selected through right click in JavaScript? I tried with `onpaste` event and all other html events available but nothing works.

Original source