Copy To Clipboard
<!DOCTYPE html>
<html>
<head>
<script>
function fnct_copy(text)
{
navigator.clipboard.writeText(text);
}
</script>
</head>
<body>
<input id="input_id" value="testo da copiare">
<input id="btn_id" type="button" value="Copy to clipboard">
<script>
document.getElementById("btn_id").addEventListener("click", function(){fnct_copy(document.getElementById("input_id").value);})
</script>
</body>
</html>
<pre><code id="code1">Questo è un testo di esempio.</code></pre>
<button class="" onclick="copyCode('code1', this)">Copia Codice</button>
function copyCode(codeId, button) {
// Seleziona il codice da copiare
const code = document.getElementById(codeId).innerText;
// Crea un elemento temporaneo per copiare il testo
const tempElement = document.createElement("textarea");
tempElement.value = code;
document.body.appendChild(tempElement);
tempElement.select();
document.execCommand("copy");
document.body.removeChild(tempElement);
// Cambia l'etichetta del pulsante
button.innerText = "Codice copiato!";
// Ripristina il testo dopo 2 secondi
setTimeout(() => {
button.innerText = "Copia Codice";
}, 2000);
}
Questo è un testo di esempio.