1function copyToClipboard(text) {
2 const elem = document.createElement('textarea');
3 elem.value = text;
4 document.body.appendChild(elem);
5 elem.select();
6 document.execCommand('copy');
7 document.body.removeChild(elem);
8}
1function copy() {
2 var copyText = document.querySelector("#input");
3 copyText.select();
4 document.execCommand("copy");
5}
6
7document.querySelector("#copy").addEventListener("click", copy);
1<html>
2 <input type="text" value="Hello world"(Can be of your choice) id="myInput"(id is the name of the text, you can change it later)
3<button onclick="Hello()">Copy Text</button>
4
5<script>
6 function Hello() {
7 var copyText = document.getElementById('myInput')
8 copyText.select();
9 document.execCommand('copy')
10 console.log('Copied Text')
11}
12</script>
1function copyToClipboard(text) {
2 var inputc = document.body.appendChild(document.createElement("input"));
3 userPreference = "COPIED!";
4 inputc.value = window.location.href;
5 inputc.focus();
6 inputc.select();
7 document.execCommand('copy');
8 inputc.parentNode.removeChild(inputc);
9 document.getElementById("msg").innerHTML = userPreference;
10
11 setTimeout(function(){
12 document.getElementById("msg").innerHTML = '';
13 }, 2000);
14}
15
16/* HTML */
17/* call "onclick="copyToClipboard()" */
18<button onclick="copyToClipboard()" class="copy-url-btn">Copy link to page</button>
19
1const copyToClipboard = (text) => navigator.clipboard.writeText(text);
2
3copyToClipboard("Hello World");
4