2

I have 2 pre blocks, each of them is wrapped with a div and has a copy button.

<div class="code">
    <a class="copy">copy</a>
    <pre>content of 1st pre</pre>
</div>

<div class="code">
    <a class="copy">copy</a>
    <pre>content of 2nd pre</pre>
</div>
$('.code').on('mouseenter', function() {
    var copy_button = $(this).find('.copy');
    var clip = new ZeroClipboard(copy_button, {moviePath: 'ZeroClipboard.swf'});
    var content = $(this).find('pre').text();

    // at this point, content is always right
    // alert(content);

    clip.on('mousedown', function(client, args) {
        // the content doesn't get updated here
        alert(content);

        clip.setText(content);
    });
});

The problem is, it seems that it always copys the conent of the first-mouseentered-div.

Say I first mouseentered div2, and clicked copy, the content (content of 2nd pre) is copied fine. But then when I try to copy the first pre, the content doesn't get updated, it's still content of 2nd pre.

What am I doing wrong here? How can I fix this?

2 Answers 2

0

OK, I found another jQuery plugin - zClip, which is built using the Zero Clipboard library. It's much easier to use and configure.

$('.copy').zclip({
    path: 'ZeroClipboard.swf',
    copy: function() {
        var tocopy = $(this).parent().find('pre').text();
        // formatting content
        // ...
        return tocopy;
    },
    beforeCopy:function(){
        // do something before copy
    },
    afterCopy:function(){
        // do something after copy
    }
});
0

You keep on adding more and more events on mouse enter. This should unbind events so you don't add each time:

.on('mouseout', function(){
    $(this).unbind();
});
2
  • ok, that is only related to div.code. it doesn't even matter if I unbind or not. that has nothing to do with the copied-content problem. Jan 28, 2013 at 16:39
  • Jeepers. Just can't get that mousedown event to fire. Try adding a function round it: (function(content){ })(content); to protect the variable. Jan 28, 2013 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.