8

When a specific textarea is copied I want it to be hidden AFTER being copied. I have the following code:

    $('#textarea20').on('copy', function() {    
    $('#textarea20').hide();
});

As you see this will hide it then the browser will try to copy a field that is hidden so it will copy nothing to the clipboard. Can you think of any way I can handle this? Any help is appreciated.

1 Answers1

5

Deferring the hide() by a few milliseconds should work.

Demo: http://jsfiddle.net/techfoobar/uxmRs/

Code:

$('textarea').on('copy', function() {    
    setTimeout(function() {
        $('textarea').hide();
    }, 10);
});
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • 1
    +1 - Might be worth using a reference to `this` within the function just in case there are multiple `textareas` on the page (I know the OP used an id in his selector and it doesn't apply there but it may help others who come across this example) – billyonecan Sep 20 '12 at 11:38
  • 1
    Yes, true. Plus, saving the reference would also mean jQuery does not have to traverse the DOM to find the element(s) inside the setTimeout function. – techfoobar Sep 20 '12 at 11:39
  • Perfect. Thanks for your help and quick response! I really appreciate it –  Sep 20 '12 at 11:46