HTML
<input type="text" class="form-control copytext" value="Hello I'm some text">
<button class="copyButton">Copy Textarea</button>
JS
//COPY DATA CLIPBOARD
var copyTextareaBtn = document.querySelector('.copyButton');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.copytext');
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');
}
});
<input type="text" class="form-control copytext" value="Hello I'm some text">
<button class="copyButton">Copy Textarea</button>
JS
//COPY DATA CLIPBOARD
var copyTextareaBtn = document.querySelector('.copyButton');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.copytext');
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');
}
});
How do I copy to the clipboard in JavaScript
No comments:
Post a Comment