Copiar texto especificado al Portapapeles al hacer clic en

Tiempo de ejecución: 30 minutos. Empezar

Autor: Adam Quinlan
Views Total: 413
Sitio oficial: Ir a la web
Actualizado: August 28, 2018
Licencia: MIT

Vista prévia

Copiar texto especificado al Portapapeles al hacer clic en

Descripción

Una función auxiliar de copia a portapapeles que permite al usuario copiar el texto especificado en su portapapeles con soporte de devolución de llamada.

Funcionamiento

Cree un elemento para copiar el texto en el portapapeles una vez hecho clic.

<button id="example">Clip To Clipboard</button>

La copia principal a la función auxiliar del portapapeles.

let createCopy = function(textToCopy, triggerElementId, callback = null) {

//add event listner to elementtrigger

let trigger = document.getElementById(triggerElementId);

trigger.addEventListener("click", function() {


//create the readonly textarea with the text in it and hide it


let tarea = document.createElement("textarea");


tarea.setAttribute("id", triggerElementId + "-copyarea");


tarea.setAttribute("readonly", "readonly");


tarea.setAttribute(



"style",



"opacity: 0; position: absolute; z-index: -1; top: 0; left: -9999px;"


);


tarea.appendChild(document.createTextNode(textToCopy));


document.body.appendChild(tarea);



//select and copy the text in the readonly text area


tarea.select();


document.execCommand("copy");



//remove the element from the DOM


document.body.removeChild(tarea);



//fire callback function if provided


if (typeof callback === "function" && callback()) {



callback();


}

});
};

Habilite el botón para copiar el texto ' WIKICSS. COM ' en el portapapeles.

createCopy('WIKICSS.COM', 'example', function () {

alert('Copied!');
});

Te puede interesar: