Lucas
Lucas

Reputation: 2994

How to copy value of textarea into clipboard and remove textarea in the same time?

I want to create a function that deletes a textarea after user copy value of this textarea into clipboard:

$("textarea").bind('copy', function() {  
             this.remove();
});

The problem is - when I press CTRL+C script deletes textarea before it allows copy value to clipboard.

How to copy value to clipboard and then delete textarea?

Upvotes: 0

Views: 214

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78595

You could force the remove event to happen in the next event loop using setTimeout:

$("textarea").on('copy', function() {  
    var textarea = $(this);
    setTimeout(function() {
        textarea.remove();    
    });
});

jsFiddle Example

(Note I replaced your bind with on, as recommended from jQuery 1.7 onwards, and I referenced $(this) instead of this to ensure the jQuery event was used. You were probably using ChildNode.remove unknowingly)

Upvotes: 3

Related Questions