-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.copy-to-clipboard.js
44 lines (41 loc) · 1.22 KB
/
jquery.copy-to-clipboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Copyright (c) 2016 Milan Kyncl
* Licensed under the MIT license.
*
* jquery.copy-to-clipboard plugin
* https://github.com/mmkyncl/jquery-copy-to-clipboard
*
*/
$.fn.CopyToClipboard = function() {
var textToCopy = false;
if(this.is('select') || this.is('textarea') || this.is('input')){
textToCopy = this.val();
}else {
textToCopy = this.text();
}
CopyToClipboard(textToCopy);
};
function CopyToClipboard( val ){
var hiddenClipboard = $('#_hiddenClipboard_');
if(!hiddenClipboard.length){
$('body').append('<textarea readonly style="position:absolute;top: -9999px;" id="_hiddenClipboard_"></textarea>');
hiddenClipboard = $('#_hiddenClipboard_');
}
hiddenClipboard.html(val);
hiddenClipboard.select();
document.execCommand('copy');
document.getSelection().removeAllRanges();
hiddenClipboard.remove();
}
$(function(){
$('[data-clipboard-target]').each(function(){
$(this).click(function() {
$($(this).data('clipboard-target')).CopyToClipboard();
});
});
$('[data-clipboard-text]').each(function(){
$(this).click(function(){
CopyToClipboard($(this).data('clipboard-text'));
});
});
});