Mark FIDDLE how to do this in the input and text box. Both mouse and keyboard events are supported.
HTML:
<p> <input class="js-copytextinput" value="http://www.stackoverflow.com"></input> <button class="js-textinputcopybtn">Copy Text Input</button> </p> <p> <textarea class="js-copytextarea">http://www.stackexchange.com</textarea> <button class="js-textareacopybtn">Copy Textarea</button> </p>
JS:
//textinput copy var copyTextinputBtn = document.querySelector('.js-textinputcopybtn'); copyTextinputBtn.addEventListener('click', function(event) { var copyTextinput = document.querySelector('.js-copytextinput'); copyTextinput.select(); try { var successful = document.execCommand('copy'); var msg = successful ? 'successful' : 'unsuccessful'; console.log('Copying text input command was ' + msg); } catch (err) { console.log('Oops, unable to copy'); } });
Source: Excerpt from a response provided by Dean Taylor, with minor changes.
You can link the copied copy and cut events in jQuery like this,
$(".select").bind({ copy : function(){ $('span').text('copy behaviour detected!'); }, paste : function(){ $('span').text('paste behaviour detected!'); }, cut : function(){ $('span').text('cut behaviour detected!'); } });
Check Fiddle for snapping copies, cut and paste events through jQuery.
- Both key and mouse events are cut, copy, and paste related.
$(document).ready(function() {
http://www.stackoverflow.comhttp://www.stackexchange.comhttp://www.stackoverflow.comhttp://www.stackexchange.com
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <input class="js-copytextinput" value="http://www.stackoverflow.com"></input> <button class="js-textinputcopybtn">Copy Text Input</button> </p> <p> <textarea class="js-copytextarea">http://www.stackexchange.com</textarea> <button class="js-textareacopybtn">Copy Textarea</button> </p>
Hope this helps.
source share