Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste event in Javascript

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.

like image 781
Karthik Yan Avatar asked May 31 '12 12:05

Karthik Yan


People also ask

What is Paste event?

The paste event fires when the user initiates a paste action through the browser's user interface. The original target for this event is the Element that was the intended target of the paste action. You can listen for this event on the Document interface to handle it in the capture or bubbling phases.


4 Answers

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.

like image 70
Dmitry Pashkevich Avatar answered Oct 12 '22 07:10

Dmitry Pashkevich


The event isn't exposed by default as "onpaste" IIRC. You can do it quite simply in jQuery by issuing

jQuery(document).bind('paste', function(e){ alert('paste event caught') });
like image 27
Jeff Watkins Avatar answered Oct 12 '22 07:10

Jeff Watkins


I was surprised question #4532473 got closed unanswered about what happens if you want to capture the afterpaste event. As this is probably the problem half of the cases a possible approach in firefox (tested) is to register an oninput event right inside the onpaste handler and remove the oninput handler as soon as it's done executing.

In ie the onpropertychange should be used instead of oninput. (not tested)

like image 1
CodeFan Avatar answered Oct 12 '22 08:10

CodeFan


Nice pure JS solution (as requested...) is available on the Mozilla dev site

<!DOCTYPE html>
<html>
<head>
<title>onpaste event example</title>
</head>

<body>
<h1>Play with this editor!</h1>
<textarea id="editor" rows="3" cols="80">
Try pasting text into this area!
</textarea>

<script>
function log(txt) {
  document.getElementById("log").appendChild(document.createTextNode(txt + "\n"));
}

function pasteIntercept(evt) {
  log("Pasting!");
}

document.getElementById("editor").addEventListener("paste", pasteIntercept, false);
</script>

<h2>Log</h2>
<textarea rows="15" cols="80" id="log" readonly="true"></textarea>
</body>
</html>
like image 1
Louis Maddox Avatar answered Oct 12 '22 08:10

Louis Maddox