Skip to content

Instantly share code, notes, and snippets.

@sushat4692
Created June 12, 2019 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sushat4692/ded8964478f8c0ae5aea58c6b1799771 to your computer and use it in GitHub Desktop.
Save sushat4692/ded8964478f8c0ae5aea58c6b1799771 to your computer and use it in GitHub Desktop.
/**
* Copy
* Don't use for input tags
*
* e.g.
* <span id="copy-target">Copied text</span>
* <button class="copy-button" data-target="#copy-target">Copy</button>
*/
$('.copy-button').on('click', function (e) {
e.preventDefault();
const target = $($(this).data('target'));
if (! target || ! target.length) {
return
}
var range = document.createRange();
var targetNode = target.get(0)
range.selectNode(targetNode.firstChild);
window.getSelection().addRange(range);
document.execCommand('copy');
window.alert('Copied');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment