<!DOCTYPE html>
<html>
<head>
<title>do copy to the clipboard in javascript</title>
<style>
</style>
</head>
<body>
<p>
<textarea class="js-copytextarea">Hello I'm some text</textarea>
</p>
<p>
<button class="js-textareacopybtn">Copy Textarea</button>
</p>
<script type="text/javascript">
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.js-copytextarea');
copyTextarea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
});
</script>
</body>
</html>
the textarea must be visible.
Reference:
http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
Comments
Post a Comment