I want to be able to copy the text of a button to the clipboard. I am able to retrieve the innerText of the button and log it to console but I am not able to add it to the selection and then ultimately add it to the clipboard with 'document.execCommand("copy");'. Any ideas?
$(document).ready(function() {
$('button').on('click', function() {
var copyText = this.innerText;
console.log(copyText);
copyText.select;
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div>
<ul id="test">
<li>
<button class="copy-button" id="button1">Test1</button>
</li>
<li>
<button class="copy-button" id="button2">Test2</button>
</li>
<li>
<button class="copy-button" id="button3">Test3</button>
</li>
</ul>
</div>