-
-
Save Darkilen/a716f7c13d99954d33a671aca0af5672 to your computer and use it in GitHub Desktop.
Copy to clipboard without input
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
</head> | |
<body> | |
<div id="secretInfo" style="display: none;">secret info</div> | |
<button type="button" id="btnCopy">Copy hidden info</button> | |
<script type="text/javascript"> | |
var $body = document.getElementsByTagName('body')[0]; | |
var $btnCopy = document.getElementById('btnCopy'); | |
var secretInfo = document.getElementById('secretInfo').innerHTML; | |
var copyToClipboard = function(secretInfo) { | |
var $tempInput = document.createElement('INPUT'); | |
$body.appendChild($tempInput); | |
$tempInput.setAttribute('value', secretInfo) | |
$tempInput.select(); | |
document.execCommand('copy'); | |
$body.removeChild($tempInput); | |
} | |
$btnCopy.addEventListener('click', function(ev) { | |
copyToClipboard(secretInfo); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment