Copiar texto al portapapeles JavaScript
//As simple as this
navigator.clipboard.writeText("Hello World");
Silly Sable
//As simple as this
navigator.clipboard.writeText("Hello World");
function copyToClipboard(text) {
const elem = document.createElement('textarea');
elem.value = text;
document.body.appendChild(elem);
elem.select();
document.execCommand('copy');
document.body.removeChild(elem);
}
function copy() {
var copyText = document.querySelector("#input");
copyText.select();
document.execCommand("copy");
}
document.querySelector("#copy").addEventListener("click", copy);
<html>
<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)
<button onclick="Hello()">Copy Text</button>
<script>
function Hello() {
var copyText = document.getElementById('myInput')
copyText.select();
document.execCommand('copy')
console.log('Copied Text')
}
</script>
Also works on safari!
function copyToClipboard() {
var copyText = document.getElementById("share-link");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
}
navigator.clipboard.writeText("your text here")