Angelus
Angelus

Reputation: 81

How to trigger Ctrl+C key event with jQuery?

I want to simulate Ctrl+C to copy the text from a page. I first tried this:

$('#codetext').click( function() {
  $("#codetext").trigger({
    type:  'keydown',
    which:  99
  });
}

HTML:

<input type='text' id='codetext'>

I have also tried using $(this) instead of the selector, but the input element also has focus on it, so it doesn't run.

Upvotes: 8

Views: 14414

Answers (4)

Julian
Julian

Reputation: 1612

Ctrl+C event mainly used for copy content.

I tried to trigger this event on summernote.

While trying, I could know that document.execCommand('copy') can touch this problem.

Like this you may trigger cut, paste event by using document.execCommand('cut') and document.execCommand('paste').

Upvotes: 2

Tim Down
Tim Down

Reputation: 324477

You cannot trigger a cut, copy or paste programmatically in JavaScript (at least, not in most browsers). These actions can only come from the user. If you need to do this you'll need some kind of hack like the Flash-based things in other answers but I wouldn't even rely on them working forever.

Upvotes: 2

Mottie
Mottie

Reputation: 86403

Check out ZeroClipboard... I think it works, but I haven't tested it.

Upvotes: 4

Ross
Ross

Reputation: 17967

not sure how to trigger ctrl+c, but there's a JQuery clipboard plugin that may be of some use:

http://plugins.jquery.com/project/copy

$("#elmID").copy() // copy all text inside #elmID.

Upvotes: 3

Related Questions