ccc

JS: Copiar contenido al portapapeles

<!DOCTYPE html>
<html>
<head>
<script>
function copyToClipboard() {
  var copyText = document.getElementById("txtToCopy");
  copyText.select();
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/
  document.execCommand("copy");
  console.log("Copiado: " + copyText.value);
}
</script>
</head>

<body>
<input type="text" value="" id="txtToCopy">
<button onclick="copyToClipboard()">Copiar texto</button>
</body>
</html>


Otro ejemplo:
<a href="javascript: void(0)" onclick="copiarAlPortapapeles('En un lugar de la mancha')" class="li"><span class="ico ico-link"></span></a>

<script>
function copiarAlPortapapeles(itemToCopy) {
        var aux = document.createElement("input");
aux.setAttribute("value", itemToCopy);
        document.body.appendChild(aux);
        aux.select();
        document.execCommand("copy");
        document.body.removeChild(aux);
    }
</script>

No hay comentarios:

Publicar un comentario