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.

Ankur Aggarwal
  • 2,993
  • 5
  • 30
  • 56
  • 1
    What element holds this `para`? – Guruprasad J Rao Jul 15 '16 at 07: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? – T.J. Crowder Jul 15 '16 at 07:33
  • Check this http://stackoverflow.com/questions/15516218/how-to-handle-undo-redo-event-in-javascript – Ankur Bhadania Jul 15 '16 at 07: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. – Dan_ Jul 15 '16 at 07:34
  • @GuruprasadRao can be anything on which undo works. In my case its contenteditable div – Ankur Aggarwal Jul 15 '16 at 07:35
  • It should work with normal `ctrl+z` right? Why need a JS function for this? – Guruprasad J Rao Jul 15 '16 at 07:37
  • I think you can handle `ctrl+z` event. And I don't know, probably stupid approach: to compare old and new texts. The difference between texts will be the thing that user "undone" – S Panfilov Jul 15 '16 at 07:37

1 Answers1

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>
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • You haven't understood my question. I want to get the text which will be back on undo not the undo execution command – Ankur Aggarwal Jul 15 '16 at 09:21
  • 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* – zer00ne Jul 15 '16 at 09:22
  • retrieved means retrieval in some javascript variable. Updating question with more clear explanation – Ankur Aggarwal Jul 15 '16 at 09:34
  • @AnkurAggarwal see update. I believe I understood whatever you **said**, not whatever you **meant**. – zer00ne Jul 15 '16 at 10:20