-4

I have a button on my website. This button is called copy. When the user clicks it, I want to have something be copied.

I already make the JavaScript function like this:

function copyToClipboardCrossbrowser(s, el) {}

where s is the data to be copied. How please?

Thanks

Anonymous
  • 10,924
  • 6
  • 31
  • 56
Marco Dinatsoli
  • 9,244
  • 33
  • 108
  • 224

1 Answers1

1

You can't with pure javascript, with Flash however you can work around it: https://github.com/zeroclipboard/ZeroClipboard

Example:

html

<html>
  <body>
    <button id="copy-button" data-clipboard-text="Copy Me!" title="Click to copy me.">Copy to Clipboard</button>
    <script src="ZeroClipboard.js"></script>
    <script src="main.js"></script>
  </body>
</html>

main.js

var client = new ZeroClipboard( document.getElementById("copy-button") );

client.on( "ready", function( readyEvent ) {
  alert( "ZeroClipboard SWF is ready!" );

  client.on( "aftercopy", function( event ) {
    event.target.style.display = 'none';
    alert("Copied text to clipboard: " + event.data["text/plain"]);
  });
});
Wolph
  • 69,888
  • 9
  • 125
  • 143