Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy text to clipboard using Javascript. It works on Safari (iOS) and other browsers.
window.Clipboard = (function(window, document, navigator) {
var textArea,
copy;
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.value = text;
document.body.appendChild(textArea);
}
function selectText() {
var range,
selection;
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
}
function copyToClipboard() {
document.execCommand('copy');
document.body.removeChild(textArea);
}
copy = function(text) {
createTextArea(text);
selectText();
copyToClipboard();
};
return {
copy: copy
};
})(window, document, navigator);
Clipboard.copy('text to be copied');
@rproenca

This comment has been minimized.

Copy link
Owner Author

@rproenca rproenca commented Oct 21, 2017

How to use:

Clipboard.copy('text to copy');

@kayza-media

This comment has been minimized.

Copy link

@kayza-media kayza-media commented Apr 24, 2018

awesome

@dbgupta

This comment has been minimized.

Copy link

@dbgupta dbgupta commented May 24, 2018

Thanks..
but there is an issue on iphone. Iphone keypad pops up and hides every time i click on copy button.

@1naveengiri

This comment has been minimized.

Copy link

@1naveengiri 1naveengiri commented Jun 26, 2018

not working in Iphone

@webestet

This comment has been minimized.

Copy link

@webestet webestet commented Jul 2, 2018

not working iphone 6s, iphone 7, IOS 11

@zhanfeng-z

This comment has been minimized.

Copy link

@zhanfeng-z zhanfeng-z commented Jul 5, 2018

@webestet
add a click event to the copy button,just like this:
<div class="button clipboardBtn" data-clipboard-action="copy" data-clipboard-text="{browserShareText.hideText}" on-click="console.log(1)">复制并去微信粘贴</div>
It works for me, iphone 6s, IOS 11.4

@PhGalaz

This comment has been minimized.

Copy link

@PhGalaz PhGalaz commented Aug 24, 2018

When clicked on iphone the viewport scrow down to focus my input (another input I'm using on the site). Any idea to prevent this?

@torben3d

This comment has been minimized.

Copy link

@torben3d torben3d commented Nov 6, 2018

When clicked on iphone the viewport scrow down to focus my input (another input I'm using on the site). Any idea to prevent this?

hey, I used the hacky way of positioning the element out of page flow.

position: absolute;
top: -9999px
@kbizukov

This comment has been minimized.

Copy link

@kbizukov kbizukov commented Dec 12, 2018

On iphones, to prevent keypad popup and zooming to input, use readonly attirbute on textarea

@MarkChoCho

This comment has been minimized.

Copy link

@MarkChoCho MarkChoCho commented Jan 2, 2019

awesome!!! totally helped me, Thank you!!

@carpogoryanin

This comment has been minimized.

Copy link

@carpogoryanin carpogoryanin commented Feb 25, 2019

When clicked on iphone the viewport scrow down to focus my input (another input I'm using on the site). Any idea to prevent this?

hey, I used the hacky way of positioning the element out of page flow.

position: absolute;
top: -9999px

@torben3d using this page is scrolled at the top :)

@karrramba

This comment has been minimized.

Copy link

@karrramba karrramba commented Aug 14, 2019

On iphones, to prevent keypad popup and zooming to input, use readonly attirbute on textarea

<meta name="viewport" content="width=device-width, initial-scale=1">

@Benetos

This comment has been minimized.

Copy link

@Benetos Benetos commented Sep 22, 2019

Is there a way to use this to copy multiple text boxes? From a form or something along those lines?

@SWei1234

This comment has been minimized.

Copy link

@SWei1234 SWei1234 commented Jan 7, 2021

Looks like it's not working on safari 13...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment