Raven
Raven

Reputation: 4903

document.execCommand("paste") doesn't execute

I have an input HTML element like so:

<input title="clipboard" id="clipboard-input" type="text" value="">

I have binded it's paste event with jquery to a custom code which is not relevant but this binding works when ctrl+v is pressed while the element is focused.

Now, I have created a button for triggering the paste event. It's just a simple button with click event bound to a function with body such as this:

$("#clipboard-input")[0].focus();
document.execCommand("paste");

The click event is fired when button is pressed, clipboard element get's focused, but the paste event is not triggered by execCommand. Reading the documentation this should work, as it is user-initiated action which is calling the execCommand but for some reason the execCommand returns false, meaning that it failed.

I'm looking for a cross-browser solution to this problem without using flash.

Edit:

I have created MWE (minimal non-working example) here. Try pasting something directly in the input field and the paste handler will be called, changing the text in paragraph. But when the button is pressed, the input field is focused but the execCommand fails to trigger the paste event.

Upvotes: 0

Views: 4437

Answers (1)

Airwind711
Airwind711

Reputation: 120

This is a specific command works only when the browser is in design mode that is the document is turned into an editor that exposes the RTE features and the exact syntax is

document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)

Ref : https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

Upvotes: 2

Related Questions