0

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

Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99
  • look at this answer http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript – boroboris Mar 28 '17 at 14:45
  • there is a problem with clippboard poisoning so browsers have strict rules on that – boroboris Mar 28 '17 at 14:45
  • Possible duplicate of [How do I copy to the clipboard in JavaScript?](http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – boroboris Mar 28 '17 at 14:46

1 Answers1

2

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.

Organiccat
  • 5,633
  • 17
  • 57
  • 104