Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Created November 15, 2017 09:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sanix-Darker/667dad787e7392b48baa66408d200174 to your computer and use it in GitHub Desktop.
Save Sanix-Darker/667dad787e7392b48baa66408d200174 to your computer and use it in GitHub Desktop.
[JS] Copy to clipboard
<input name="exampleClipboard" placeholder="Insert the text you want to copy" value="Example text" tabindex="1" autocomplete="off" maxlength="240" style="width:200px" type="text">
<p>
<button id="copy">
Copy text
</button>
</p>
<p>
Right click paste or Ctr + v after click Copytext button <br/>
<textarea></textarea>
</p>
<script>
function copyToClipboard() {
document.querySelector('input').select();
document.execCommand('copy');
//clear selection
if ( document.selection ) {
document.selection.empty();
} else if ( window.getSelection ) {
window.getSelection().removeAllRanges();
}
}
document.querySelector('#copy').addEventListener('click', function() {
copyToClipboard()
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment