Search code examples
javascriptzeroclipboard

ZeroClipboard - Add to values before copying


I'm using the JS version of ZeroClipboard.

When the user clicks the copy button I want to capture the data from data-clipboard-text add some extra data to this and then copy this to the clipboard.

This the code I'm currently using, How do I add to the text before it is copied to the clipboard ?

var clip = new ZeroClipboard( $("input[type='button']"), {
  moviePath: "clip/ZeroClipboard.swf"
});
clip.on( 'complete', function(client, args) {
    alert ('Copied');
});

I've tried using this before clip.on but it failed :

clip.on('dataRequested', function (client, args) {
        clip.setText(args + "NEW VALUE");
    } );

Any ideas ?

Thanks


Solution

  • Setting the clipboard text can be done using the 'copy' event handler [v2.x] :

    clip.on( "copy", function (event) {
        var clipboard = event.clipboardData;
        clipboard.setData( "text/plain", event.target.innerHTML + " <-- added this text!" );
        /*
         * for data-attribute use event.target.getAttribute("data-clipboard-text")
         */
    });
    

    For previous version [1.x] using the 'dataRequested' event handler:

    client.on( 'dataRequested', function (client, args) {
      client.setText( this.getAttribute("data-clipboard-text") + "<- Copied me!" );
      /*
       * for text inside component use this.innerHTML
       */
    });