1function copy() {
2 var copyText = document.querySelector("#input");
3 copyText.select();
4 document.execCommand("copy");
5}
6
7document.querySelector("#copy").addEventListener("click", copy);
1document.getElementById("copy-text").onclick = function() {
2 this.select();
3 document.execCommand('copy');
4 alert(window.getSelection().toString());
5}
1function copyToClipboard(text) {
2 var input = document.body.appendChild(document.createElement("input"));
3 input.value = text;
4 input.focus();
5 input.select();
6 document.execCommand('copy');
7 input.parentNode.removeChild(input);
8}
1var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
2
3copyTextareaBtn.addEventListener('click', function(event) {
4 var copyTextarea = document.querySelector('.js-copytextarea');
5 copyTextarea.focus();
6 copyTextarea.select();
7
8 try {
9 var successful = document.execCommand('copy');
10 var msg = successful ? 'successful' : 'unsuccessful';
11 console.log('Copying text command was ' + msg);
12 } catch (err) {
13 console.log('Oops, unable to copy');
14 }
15});
16
17////html code below
18<p>
19 <button class="js-textareacopybtn" style="vertical-align:top;">Copy Textarea</button>
20 <textarea class="js-copytextarea">Hello I'm some text</textarea>
21</p>
1function (container_id, as_html) {
2 /*
3 isEmpty = function (val) {
4 return (val === undefined || val == null || val.length <= 0) ? true : false;
5 };
6 */
7 as_html = isEmpty(as_html) ? false : true;
8
9 //console.log('copyToClipboard: ' + container_id);
10 var elem = document.getElementById(container_id);
11
12 // create hidden text element, if it doesn't already exist
13 var targetId = "_hiddenCopyText_";
14 var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
15 var origSelectionStart, origSelectionEnd;
16 if (isInput) {
17 // can just use the original source element for the selection and copy
18 target = elem;
19 origSelectionStart = elem.selectionStart;
20 origSelectionEnd = elem.selectionEnd;
21 //console.log ('_is_input = true');
22 } else {
23 // must use a temporary form element for the selection and copy
24 //console.log ('_is_input = false, creating a textarea = ' + targetId);
25 target = document.getElementById(targetId);
26 if (!target) {
27 var target = document.createElement("textarea");
28 target.style.position = "absolute";
29 target.style.left = "-9999px";
30 target.style.top = "0";
31 target.id = targetId;
32 document.body.appendChild(target);
33 }
34
35 var content = $.trim(as_html ? $(elem).html() : $(elem).text());
36 $(target).text( content);
37 console.log ('text_content', content);
38 }
39 // select the content
40 var currentFocus = document.activeElement;
41 target.focus();
42 target.setSelectionRange(0, content.length);
43
44 // copy the selection
45 var succeed;
46 try {
47 succeed = document.execCommand("copy");
48 } catch (e) {
49 succeed = false;
50 }
51 // restore original focus
52 if (currentFocus && typeof currentFocus.focus === "function") {
53 currentFocus.focus();
54 }
55
56 if (isInput) {
57 // restore prior selection
58 elem.setSelectionRange(origSelectionStart, origSelectionEnd);
59 } else {
60 // clear temporary content
61 $(target).text('');
62 }
63
64 return succeed;
65 }
1<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
2<p id="p1">P1: I am paragraph 1</p>
3<p id="p2">P2: I am a second paragraph</p>
4<button onclick="copyToClipboard('#p1')">Copy P1</button>
5<button onclick="copyToClipboard('#p2')">Copy P2</button>
6<br/><br/><input type="text" placeholder="Paste here for test" />