How to detect copy, paste and cut behavior with jQuery

To detect copy, paste and cut behavior, you just need to bind the corresponding event type.


$("#textA").bind('copy', function() {
    $('span').text('copy behaviour detected!')
});	
$("#textA").bind('paste', function() {
    $('span').text('paste behaviour detected!')
});	
$("#textA").bind('cut', function() {
    $('span').text('cut behaviour detected!')
});

If you are using jQuery 1.4x, it’s support the multiple events declaration like following :


$("#textA").bind({
	copy : function(){
		$('span').text('copy behaviour detected!');
	},
	paste : function(){
		$('span').text('paste behaviour detected!');
	},
	cut : function(){
		$('span').text('cut behaviour detected!');
	}
});

Try it yourself


<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

<style type="text/css">
	span{
		color:blue;
	}
</style>

</head>
<body>
  <h1>jQuery copy, paste and cut example</h1>
  <form action="#">
  	<label>TextBox : </label>
	<input id="textA" type="text" size="50" 
          value="Copy, paste or cut message here" />
  </form>
  
  <span></span>
  
<script type="text/javascript">

$(document).ready(function() {
    
	$("#textA").bind({
		copy : function(){
			$('span').text('copy behaviour detected!');
		},
		paste : function(){
			$('span').text('paste behaviour detected!');
		},
		cut : function(){
			$('span').text('cut behaviour detected!');
		}
	});

});	
</script>
</body>
</html>

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
10 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
keerthi KSimonJCrazybloggernithi Recent comment authors
newest oldest most voted
Bujji
Guest
Bujji

Hi,
I need this example, but here i cant block the captured event could u sort me out from where to start. I need to disable all the things just need to enter from the keyboard entry only. here you are captured not blocked the event is going on.
-Thanks

keerthi K
Guest
keerthi K

Can we bind these events to a folder (like d:/myfiles) instead of a html control…
Could you please give suggestions on this.

trackback
jQuery Tutorials

[…] Detect copy, paste and cut behavior How to detect copy, paste and cut behavior with jQuery. […]

uma kane
Guest
uma kane

thanks for the tutorial. had been searching for the same.

trackback
Tracking copy/paste events with jQuery and Google Analytics • Onderweg Blog

[…] code fragments) to their clipboard. Detecting copy/paste/cut events in jQuery turns out to be easy And thanks to Google Analytics event tracking API, tracking these events with GA is also easy. This […]

xxx
Guest
xxx

Does NOT work in Opera 11.00 :~(

nithi
Guest
nithi

nice demo

Crazyblogger
Guest
Crazyblogger

How can I disable whole website content from being copied? Is there a way to do this?

J
Guest
J

Is there a way to auto paste a URL link into the clipboard when a user clicks on the link?

This would be most helpful. Thanks.

Simon
Guest
Simon

Hi,
Any idea how this could be implemented using Google Tag Manager?
Thanks!