[javascript] How do you handle oncut, oncopy, and onpaste in jQuery?


Answers

Various clipboard events are available in Javascript, though support is spotty. QuicksMode.org has a compatibility grid and test page. The events are not exposed through jQuery, so you'll either have to extend the library or use native Javascript events.

Question

The jQuery documentation says the library has built-in support for the following events: blur, focus, load, resize, scroll, unload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, and error.

I need to handle cut, copy, and paste events. How best to do that? FWIW, I only need to worry about WebKit (lucky me!).

UPDATE: I'm working on a "widget" in a Dashboard-like environment. It uses WebKit, so it only really matters (for my purposes) whether these events are supported there, which it looks like they are.




As jQuery 1.7 you can use bind(...) and unbind(...) methods for attaching and removing respectively handlers.

Here are examples align your questuion:

$('#someElementId').bind('paste', function(){return false;});

- this one will block any attempts to paste from clipboard into element body. You can use also cut, copy and others as event types (see links bellow)

$('#someElementId').bind('copy', function(){return alert('Hey fella! Do not forget about copyrights!');});

So, in other cases, when you want to remove those handlers, you can use unbind() method:

$('#someElementId').unbind('copy');

Here some useful links:




Related