1var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
2
3copyTextareaBtn.addEventListener('click', function(event) {
4 var copyTextarea = document.querySelector('.js-copytextarea');
5 copyTextarea.focus();
6 copyTextarea.select();
7
8 try {
9 var successful = document.execCommand('copy');
10 var msg = successful ? 'successful' : 'unsuccessful';
11 console.log('Copying text command was ' + msg);
12 } catch (err) {
13 console.log('Oops, unable to copy');
14 }
15});
16
17////html code below
18<p>
19 <button class="js-textareacopybtn" style="vertical-align:top;">Copy Textarea</button>
20 <textarea class="js-copytextarea">Hello I'm some text</textarea>
21</p>