2

i posted here an example of my problem and not my real code (too much code...). The button "copy to clipboard" doesnt copy anything on the first click... only on the second:( Please help

JSP file

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<button onclick="addCopyButton()">Show copy Button</button>
<div id="theCopyButton"></div>

<!-- javascript sources -->
<script src="resources/js/jquery.min.js"></script>
<script src="resources/js/copy.js"></script>
<script src="resources/js/ZeroClipboard.min.js"></script>
</body>
</html>

JS file

$(document).ready(function(){
    ZeroClipboard.config( { moviePath: '/goblin/resources/flash/ZeroClipboard.swf' } );
});

function addCopyButton(){
    $('#theCopyButton').html('<br/><button type="button" onclick="toClipboard()">Copy to clipboard</button>');
}

function toClipboard(){
    $('#theCopyButton').attr('data-clipboard-text', 'dynamic text');
    var zeroClipboard = new ZeroClipboard($("#theCopyButton"));
}
Erez
  • 502
  • 3
  • 6
  • 17

1 Answers1

2

You can modify your code as follows.

$(document).ready(function () {

        this.zero_clipboard = new ZeroClipboard($(".copy-button")).on('load', function (client, args) {
        }).on('complete', function (client, args) {   
            client.setText(args.text);
        }).on( 'mouseover', function (client, args) {
            client.setText(args.text);
        });


    });
Pankaj Sharma
  • 254
  • 1
  • 10
  • I tried the code but is it not working . I have the same issue. Please check this post as well http://stackoverflow.com/questions/35461387/zeroclipboard-not-copying-on-first-click?noredirect=1#comment58619253_35461387 – s_k_t Feb 17 '16 at 16:09