0

I have this text in a textbox. The document body has designMode set to on. (| is the cursor)

<div><h3>Hi there</h3><p>Welcome| to the page</p></div>

After clicking a button, the text writes the innerHTML of a div.

Question: From the position of the cursor in the textbox, can I set the cursor to that same position in the DOM.


Hi there

Welcome| to the page


Thanks.

gkiely
  • 2,987
  • 1
  • 23
  • 37
  • possilbe duplicate of [http://stackoverflow.com/q/512528/474535](http://stackoverflow.com/q/512528/474535) – bart s Mar 07 '13 at 13:57
  • Nah that is setting the cursor position in a textbox, I want to set it in the DOM that is in designMode. – gkiely Mar 07 '13 at 13:59
  • 1
    You mean you are editing the html text and at the same time you want to update the rendered html with the same cursor position? – bart s Mar 07 '13 at 14:06

1 Answers1

0

Depends on how bad you want to do this. The closest method that I can think of is setting a span at cursor position in the textbox and then setting the cursor to the position of that span.

  • Use jQuery Caret to get the position of the cursor in a textbox and save it in a variable(Caret uses $.browser to get browser info which is removed from jQuery 1.9.1 and above and moved in to a separate plugin. So you will need to use the latest of 1.8)
  • On Button click insert <span id='cur-pos'></span> at the index of the string(value stored through Caret)
  • After the html is updated get position of span and move the cursor to that position.
Jehanzeb.Malik
  • 3,332
  • 4
  • 25
  • 41