9

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)

3
  • 2
    possible duplicate of Unable to get document.execCommand('undo') working in the same manner across browsers Commented Aug 5, 2013 at 9:24
  • That question contains the answer to my question (use execCommand). But finding that question requires already knowing to search for "execCommand", which is what I didn't know. So I still think it could be worthwhile to have this question here with an answer. But whatever others think... Commented Aug 5, 2013 at 9:30
  • 1
    possible duplicate, does not mean it is a duplicate per se, it's just a flag for moderators to take a look, and for future people landing here to have an extra link to check out. Commented Aug 5, 2013 at 9:39

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed it is now. :) But back in 2013 to late 2017 it wasn't.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.