3

If I want to know when an editable field (say, an input text field or a contenteditable div), I know that I can use the keyup event.

However, I can only see what the new text is, and what the text was prior to the edit, but I would like to get some additional information about the edit.

For example, one case where this would be ambiguous would be if the input text was originally a and the new text was aa. Then:

  • It could be the case that the user put the cursor before the a, and then typed another a.
  • It could also be that the user put the cursor after the a, and then typed another a.
  • It could even be that the user highlighted the a, then hit Ctrl+V (with aa in the clipboard).

I would like to be able to distinguish all the cases. For example, if I could get information of the form "insert a at position 0" or "delete range 0-1 while inserting aa in its place" it would be perfect for my purposes.

Is it possible to get this kind of information?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tjhance
  • 961
  • 1
  • 7
  • 14

1 Answers1

0

You could 'diff' the text before and after an edit, however, this will not allow you to distinguish between all of the cases you have mentioned. The only mechanism I can think of is to monitor the cursor (or caret) position each time a keydown event occurs, as described in this answer:

Get caret position in HTML input?

Community
  • 1
  • 1
ColinE
  • 68,894
  • 15
  • 164
  • 232