-
-
Save rproenca/64781c6a1329b48a455b645d361a9aa3 to your computer and use it in GitHub Desktop.
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'); |
On iphones, to prevent keypad popup and zooming to input, use readonly
attirbute on textarea
awesome!!! totally helped me, Thank you!!
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 :)
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">
Is there a way to use this to copy multiple text boxes? From a form or something along those lines?
Looks like it's not working on safari 13...
Only support iOS>=10
.
https://stackoverflow.com/questions/34045777/copy-to-clipboard-using-javascript-in-ios
Thanks!
That cool. Thanks!
hey, I used the hacky way of positioning the element out of page flow.