0

Demo Playground

(Try to write something in the demo and see this unwanted behaviour)

A div content can be changed when user is typing in it, and the text is evaluated and some words get marked (wrapper element), so the HTML of the div is changed and user is loosing the caret position.

I can know what the caret offset was just before the HTML change, but I don't know how to restore the caret to that place, once the HTML has changed, the Range object means nothing...

Thanks for any help!

vsync
  • 118,978
  • 58
  • 307
  • 400

1 Answers1

0

Solution - http://jsfiddle.net/UuYQH/194/

I've found somewhere online a save & load methods for selection and incorporated them in my code and it works great!

vsync
  • 118,978
  • 58
  • 307
  • 400
  • Thanks, your `save` and `load` caret position functions are awesome. – vsync Jan 08 '14 at 10:36
  • @TimDown - there is a bug in your caret saving method, try to delete a character (without selecting it): http://jsfiddle.net/T6yLs/1/ – vsync Jan 09 '14 at 16:58
  • @TimDown - you probably use Chrome. I use FF, and it gives [0,0] upon deletion. – vsync Jan 09 '14 at 18:37
  • 1
    You're right, there seems to be a bug. I've written simpler code to do this since, so rather than try and debug the old code, here's your fiddle with the newer code, taken from here: http://stackoverflow.com/a/13950376/96100. http://jsfiddle.net/T6yLs/3/ – Tim Down Jan 09 '14 at 23:49