I'm trying to use JavaScript to copy text to clipboard. Here is what I've got so far
var copyTextareaBtn = document.querySelector('#copy');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.getElementById('toCopy');
copyTextarea.focus();
copyTextarea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
alert("Link copied!");
} catch (err) {
alert("Unable to copy!");
}
});
I get the error that copyTextarea.select is not a function. Why is that? I'm 100% sure that JavaScript does have a select method.