Created
June 12, 2019 00:41
-
-
Save sushat4692/ded8964478f8c0ae5aea58c6b1799771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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