Reputation: 1
I have some content copied on the clipboard. I'd like that to get pasted as an input text when I press a button called PASTE . I don't know how to do this using CSS or js.
I've tried this:
<script type="text/javascript">
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);
}
</script>
<p id="p1">hola como estas</p>
<button onclick="copiarAlPortapapeles('p1')">Copiar P1</button>
<br/><br/>
<input type="text" placeholder="Pega aquí para probar" />
but it just works to copy content to the clipboard, I cannot find any solution how to do.
thanks for help
Upvotes: 0
Views: 1276
Reputation: 5651
You can't actually grab the clipboard data without direct user input. There are some functions that will attempt it, but most are blocked by browser security.
The reason for this is, let's say I go to a "bad" site. I have copied my credit card number or social security number into memory. This site grabs that data without me knowing, when all I do is click to see an image.
This is inherently unsafe and not suggested at any level.
That all being said, there are workarounds if you use plugins, flash, or the data is being moved from one part of the site to another (like drag and drop). Pasting content from OUTSIDE the app or page itself via a single click will not work though.
Upvotes: 2