There is at least one way to achieve this without using Flash: using the copy
command % This command will copy to the clipboard the text that is selected on the page at the moment of execution.
So you can create a small function that:
Create a temporary text field (which will not be visible).
Assign the content of the element to be copied to the value of the text field.
Select the content of the text field.
Execute the copy: document.execCommand("copy")
command.
Destroy / Delete the temporary field.
Then the content of the selected element will be in the clipboard. The code would be like this:
function copiarAlPortapapeles(id_elemento) {
// Crea un campo de texto "oculto"
var aux = document.createElement("input");
// Asigna el contenido del elemento especificado al valor del campo
aux.setAttribute("value", document.getElementById(id_elemento).innerHTML);
// Añade el campo a la página
document.body.appendChild(aux);
// Selecciona el contenido del campo
aux.select();
// Copia el texto seleccionado
document.execCommand("copy");
// Elimina el campo de la página
document.body.removeChild(aux);
}
The main problem with this solution is that not all browsers support this command , although it can be used in the main ones from:
- Chrome 43
- Internet Explorer 10
- Firefox 41
A demo of the code in operation:
function copiarAlPortapapeles(id_elemento) {
var aux = document.createElement("input");
aux.setAttribute("value", document.getElementById(id_elemento).innerHTML);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
}
<p id="p1">P1: Soy el primer párrafo</p>
<p id="p2">P2: Soy el segundo párrafo</p>
<button onclick="copiarAlPortapapeles('p1')">Copiar P1</button>
<button onclick="copiarAlPortapapeles('p2')">Copiar P2</button>
<br/><br/>
<input type="text" placeholder="Pega aquí para probar" />
You can also create an equivalent version using jQuery:
function copyToClipboard(elemento) {
var $temp = $("<input>")
$("body").append($temp);
$temp.val($(elemento).text()).select();
document.execCommand("copy");
$temp.remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="p1">P1: Soy el primer párrafo</p>
<p id="p2">P2: Soy el segundo párrafo</p>
<button onclick="copyToClipboard('#p1')">Copiar P1</button>
<button onclick="copyToClipboard('#p2')">Copiar P2</button>
<br/>
<br/>
<input type="text" placeholder="Pega aquí para probar" />
That works in a similar way to the previous example, although some errors have been reported in Safari for Mac and Chrome 64-bit (see the comments in link of the original source for more data).
Source: original response on the StackOverflow site .
EXTENSION: COPY BY MAINTAINING THE STYLES
In another question in StackOverflow en Español , a user commented that text format / styles were not copied . This is because the code above uses input text
to copy the content of the element, so the HTML code is "lost" (it is interpreted literally instead of as HTML). If instead of using a input
we use a div
editable, the format is not lost and can be copied / pasted with styles:
function ejecutar(idelemento){
var aux = document.createElement("div");
aux.setAttribute("contentEditable", true);
aux.innerHTML = document.getElementById(idelemento).innerHTML;
aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)");
document.body.appendChild(aux);
aux.focus();
document.execCommand("copy");
document.body.removeChild(aux);
}
#destino {
width:400px;
height:100px;
border:1px solid #ccc;
}
<p id="demo"><b>Caso con formato.</b></p>
<button onclick="ejecutar('demo')">Copiar</button>
<br/>
<div id="destino" contentEditable="true">
</div>