Reputation: 29585
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
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
Reputation: 1939
You can use document.execCommand
to achieve this functionallity. It is used by some HTML-editors.
And by now, this is deprecated, and should not be used.
Upvotes: 5