10 examples of 'jquery copy to clipboard' in JavaScript

Every line of 'jquery copy to clipboard' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
202function copyToClipboard(element) {
203 $(element).select()
204 document.execCommand("copy");
205}
154function copyToClipboard(str, mimetype) {
155 // Listen for 'oncopy' event
156 document.oncopy = function (event) {
157 event.clipboardData.setData(mimetype, str);
158 event.preventDefault();
159 };
160
161 // Execute browser command 'Copy'
162 document.execCommand("Copy", false, null);
163}
4function copyToClipboard(text) {
5 const onCopy = ev => {
6 ev.clipboardData.setData("text/plain", text)
7 ev.preventDefault()
8 }
9
10 document.addEventListener("copy", onCopy, { once: true })
11 document.execCommand("Copy", false, null)
12}
159function copyToClipboard(text) {
160 document.addEventListener(
161 "copy",
162 function(e) {
163 e.clipboardData.setData("text/plain", text);
164 e.preventDefault();
165 },
166 { once: true }
167 );
168 document.execCommand("copy");
169}
107function copyToClipboard(text) {
108 if (window.clipboardData && window.clipboardData.setData) {
109 /*IE specific code path to prevent textarea being shown while dialog is visible.*/
110 return clipboardData.setData("Text", text);
111
112 } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
113 var textarea = document.createElement("textarea");
114
115 textarea.textContent = text;
116 textarea.style.position = "fixed"; /* Prevent scrolling to bottom of page in MS Edge.*/
117 document.body.appendChild(textarea);
118 textarea.select();
119
120 try {
121 return document.execCommand("copy"); /* Security exception may be thrown by some browsers.*/
122 } catch (ex) {
123 console.warn("Copy to clipboard failed.", ex);
124 return false;
125 } finally {
126 document.body.removeChild(textarea);
127 }
128 }
129}
231function copyToClipboard(text) {
232 if (window.clipboardData && window.clipboardData.setData) {
233 // IE specific code path to prevent textarea being shown while dialog is visible.
234 return clipboardData.setData("Text", text);
235
236 } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
237 var textarea = document.createElement("textarea");
238 textarea.textContent = text;
239 textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
240 document.body.appendChild(textarea);
241 textarea.select();
242 try {
243 return document.execCommand("copy"); // Security exception may be thrown by some browsers.
244 } catch (ex) {
245 console.warn("Copy to clipboard failed.", ex);
246 return false;
247 } finally {
248 document.body.removeChild(textarea);
249 }
250 }
251} return {
12export function copyToTheClipboard(string: string) {
13 const doCopy = function(e: any) {
14 e.clipboardData.setData("text/plain", string);
15 e.preventDefault();
16 };
17
18 document.addEventListener("copy", doCopy);
19 document.execCommand("copy", false, null);
20 document.removeEventListener("copy", doCopy);
21}
7function copy_to_clipboard(value) {
8 const el = document.createElement('textarea');
9 el.value = value;
10 el.setAttribute('readonly', '');
11 el.style.position = 'absolute';
12 el.style.left = '-9999px';
13 document.body.appendChild(el);
14 const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
15 el.select();
16 document.execCommand('copy');
17 document.body.removeChild(el);
18 if (selected) {
19 document.getSelection().removeAllRanges();
20 document.getSelection().addRange(selected);
21 }
22}
12export default function copyToClipboard (text) {
13 if (element === null) {
14 initialize();
15 }
16
17 const scrollTop = window.scrollY || window.pageYOffset;
18 element.value = text;
19 element.focus();
20 element.setSelectionRange(0, element.value.length);
21 document.execCommand('copy');
22 window.scrollTo(0, scrollTop);
23};
3function copyToClipboard(text) {
4 function oncopy(event) {
5 document.removeEventListener("copy", oncopy, true);
6 // Hide the event from the page to prevent tampering.
7 event.stopImmediatePropagation();
8
9 // Overwrite the clipboard content.
10 event.preventDefault();
11 event.clipboardData.setData("text/plain", text);
12 }
13 document.addEventListener("copy", oncopy, true);
14
15 // Requires the clipboardWrite permission, or a user gesture:
16 document.execCommand("copy");
17}

Related snippets