1

Is it possible to get the 'text' which I will get back on undo. For Example: I have two paragraphs

Para1
Para2 <--- deleting para2 using backspace

When I will do ctrl+z or undo , para2 will be retrieved back. Is there any way in javascript to get that 'para2' will be returned in the undo option (I want to get 'para2' in some js variable. I know it will come in editor automatically) ? Search a lot but didn't get any solution. Any help will be highly appreciated

P.S: I need this because I want to remove some attributes in my html on undo.

7
  • 1
    What element holds this para? Commented Jul 15, 2016 at 7:32
  • 2
    I very much doubt there's anyway to get that information for free, you'll need to do your own tracking. But it would help if you could be more specific: Is this in a contenteditable element? A textarea? An input? Commented Jul 15, 2016 at 7:33
  • Check this stackoverflow.com/questions/15516218/… Commented Jul 15, 2016 at 7:34
  • Unless you dynamically load it into a var and then do something with it. CTRL-Z & CTRL-V / Cut and paste - is an operating systems feature and not a feature of any web language. Commented Jul 15, 2016 at 7:34
  • @GuruprasadRao can be anything on which undo works. In my case its contenteditable div Commented Jul 15, 2016 at 7:35

1 Answer 1

0

UPDATE 2

Due to OP's question changes, I will address this one last time.

P.S: I need this because I want to remove some attributes in my html on undo.

Undo Retrieves deleted text

Delete Removes text

If you have a ton of attributes in your html, use the text editor's replace feature. What text editor do you use?


UPDATE 1

Added a new function getUndone(). Do the following to test it:

  1. Delete some text.
  2. click the button.
  3. click the Get button.
  4. The undone portion of text will be back as expected.
  5. The undone text is also stored in a variable (i.e. sel).
  6. The undone text is also in clipboard.
  7. The undone text is displayed as well.

  8. Used execCommand('undo') to undo.

  9. Used execCommand('copy') to store in clipboard.
  10. Used window.getSelection() to retrieve selected text and store in sel.
  11. Used .toString() to display undone text.

execCommand API is made for creating text editors. It has a ton of methods and properties. The following Snippet has bold, italics, underline, and undo.

 document.execCommand( 'undo',false,null);

SNIPPET

<!DOCTYPE html>
<html>

<head>
  <meta charset='utf-8'>
  <style>
    body {
      font: 400 16px/1.4'serif';
    }
    #editor1 {
      border: 3px inset grey;
      height: 100px;
      width: 381px;
      margin: 10px auto 0;
    }
    fieldset {
      margin: 2px auto 15px;
      width: 358px;
    }
    button {
      width: 5ex;
      text-align: center;
      padding: 1px 3px;
    }
  </style>
</head>

<body>

  <div id="editor1" contenteditable="true">
    The quick brown fox jumped over the lazy dog.
  </div>
  <fieldset>
    <button class="fontStyle" onclick="document.execCommand('italic',false,null);" title="Italicize Highlighted Text"><i>I</i>
    </button>
    <button class="fontStyle" onclick="document.execCommand( 'bold',false,null);" title="Bold Highlighted Text"><b>B</b>
    </button>
    <button class="fontStyle" onclick="document.execCommand( 'underline',false,null);"><u>U</u>
    </button>
    <button class="fontStyle" onclick="document.execCommand( 'undo',false,null);"><b>&#10554;</b>
    </button>
    <button class="fontStyle" onclick="getUndone()">Get</button>
    <br/>
    <label for='data'>Data:
      <input id="data">
    </label>
  </fieldset>
  <script>
    function getUndone() {
      var data = document.getElementById('data');
      var sel = window.getSelection();
      document.execCommand('undo', false, null);
      document.execCommand('copy', false, null);
      data.value = sel.toString();

    }
  </script>

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

4 Comments

You haven't understood my question. I want to get the text which will be back on undo not the undo execution command
That doesn't make sense. The text in undo can be retrieved with an undo command. You said "In my case its contenteditable div" That's an example of undo text being retrieved on a contenteditable div. Did you try the Snippet? It's the blue button labeled Run code snippet
retrieved means retrieval in some javascript variable. Updating question with more clear explanation
@AnkurAggarwal see update. I believe I understood whatever you said, not whatever you meant.

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.