A demo for the copyjs library.
Cool, right? It's easy! Paste the following into your HTML file...
<script src="https://cdn.jsdelivr.net/gh/codingjlu/copyjs@latest/copy.min.js"><script>
In your JavaScript code, simply call the function:
copyjs("Your text to copy!");
There are also several options:
copyjs("<h1 style="color:red">Some HTML!</h1>", { html: true, reSelect: true, copyFromSelector: false });
Use HTML if you want to copy HTML, reSelect if you want every time you copy, it rehighlights what the user highlighted, and copyFromSelector if you want to copy the text of an element and locate it using a selector. (Turn on HTML to copy the HTML as well.)