Just include the library (v0.2.3
, ~1kB!):
<script src="https://cdn.rawgit.com/ryanpcmcquen/simpleJsCopy/3639e551f1fb1b93821c70856dacce5f2318c37d/simpleJsCopy.min.js"></script>
Then use the following elements:
<textarea>
with the classtext-to-copy
(can bereadonly
but notdisabled
)<button>
with the classjs-copy-btn
The library takes cares of the rest.
📰 You don't have to write any JavaScript to use it!
=====
Almost everywhere!
Desktop:
- Chrome/Chromium 42+
- Firefox 41+
- Internet Explorer 9+
- Opera 29+
Android:
- Chrome 42+
- Firefox 41+
Everywhere else (meaning iOS), it will select the text. The library detects iDevices and will change the inital button text to Select text
(to avoid confusing the user). After clicking it will say either Now tap 'Copy'
(iPhone) or Now tap the text, then 'Copy'
(iPad), to account for the strange behavioral differences of these devices.
On desktop Safari (any version), the button will change to Press Command + C to copy
.
On failure the button will say Please copy manually
.
=====
.text-to-copy {
opacity: 0;
position: absolute;
}
Because it will not work with display: none;
or visibility: hidden;
. 👍
Note that you should also supplement that CSS with something like this helper library, otherwise people on iOS will not be able to copy at all since Safari doesn't support cool stuff: