user984003
user984003

Reputation: 29585

textarea: button to trigger native undo/redo

I have a textarea. It has default undo/redo functionality, either with ctrl-z/y or by right-clicking and choosing copy/paste.

I want to create an undo/redo button and trigger the native undo/redo. I'm not sure how to trigger this. (Was surprised that my users don't know ctrl-z)

Upvotes: 9

Views: 3775

Answers (2)

Aziz Bhavnagarwala
Aziz Bhavnagarwala

Reputation: 1

You can create functions to trigger undo and redo functionality on button click.

Undo button functionality as follows:

const handleUndoButtonClick = () => {
  document.execCommand('undo', false, '');
};

Redo button functionality as follows:

const handleRedoButtonClick = () => {
  document.execCommand('redo', false, '');
};

Call handleUndoButtonClick() on undo button click and call handleRedoButtonClick() on redo button click.

Upvotes: 0

NoLifeKing
NoLifeKing

Reputation: 1939

You can use document.execCommand to achieve this functionallity. It is used by some HTML-editors.

execCommand compatibility

And by now, this is deprecated, and should not be used.

Upvotes: 5

Related Questions