Search code examples
jquerycopyjquery-dialogzeroclipboard

zeroclipboard copy text inside jquery dialog doesn't work


I am using jquery ui dialog in my page and surprisingly the zeroclipboard copy-to-clipboard functionality does not work from within the jquery dialog box.

This is my entire code...

<html>
<head>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css" rel="stylesheet" 
    type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
    <script type="text/javascript" src="http://davidwalsh.name/dw-content/ZeroClipboard.js"></script>

    <script type="text/javascript"> 
    $(function(){
        $('#ex1').click(function(){
            var div = $('#div1');
            div.dialog(
                {
                    title:'Dialog1',
                    width: 300,
                    height: 150,
                    closeOnEscape: false
                });
        });
    });

    function toClipboard(me, msg_id) {
                        ZeroClipboard.setMoviePath('http://davidwalsh.name/dw-content/ZeroClipboard.swf');
                        var clip = new ZeroClipboard.Client();
                        //clip.setHandCursor( true );
                        var txt = $("#msg_p_span_"+msg_id).html();

                        console.log("Text: "+txt);
                            clip.addEventListener('mousedown',function() {
                            clip.setText(txt);
                            console.log("Copied");
                            });
                            clip.addEventListener('complete',function(client,text) {
                            alert('copied: ' + text);
                            });
                        clip.glue(me);
                }
    </script>

<title>jQuery UI dialog extra demo</title>
</head>
<body>

  <span id="msg_p_span_1" style="display:none;">Testing the clipboard copy 1.</span>
  <span title="Copy to clipboard" style="cursor: pointer; text-decoration:underline;" onmouseOver="toClipboard(this, 1)">Copy</span>
  <button id="ex1">Launch dialog</button>

<div id="div1" style="display:none;">

<p style="padding: 10px 3px; font-size: 12px;" id="msg_p_2">
<span id="msg_p_span_2">Testing the clipboard copy 2.</span>
<span style="float: right; width: 25px; margin-right: 10px;">
<span title="Copy to clipboard" style="cursor: pointer; text-decoration:underline;" onmouseOver="toClipboard(this, 2)">Copy</span>
</span>
</p>

</div>  
</body>
</html>

If i click on the first 'Copy' link, the text is getting copied well and an alert is generated. But when i launch the jquery dialog box and click on the 'Copy' link inside it, the text is not copied.

Probably someone can reproduce the issue with the exact above code (copy-paste) and find it.

Note: I am following the core example at Davidwalsh site


Solution

  • Well, i figured out the solutions to my problem, after a lot of google search.

    As stated in these official google code project pages: here and here (Sorry, the links were broken), i found that it is recommended to give the element that we set as copy element (in my case, it is the "Copy" link inside the jquery dialog box), a higher value of z-index (say 9999).

    Another way i found was that if i gave a less z-index value to the jquery dialog div, the copy-to-clipboard functionality is working fine.

    Hope this helps someone in the future.