0

I'm trying to keep the cursor at specific character in contentEditable div when .focus() event occurs.

That is I've content editable div(.textA) with following content:

<div class='textA' contentEditable='true'>
Hello There to all <div class='xyz' contentEditable='true'>SO</div> Users.<br>
Can you all Please <div class='abc'>Solve</div> This for me?<br>
<br>
<br>
<div class='xyz'>VT</div>
</div>

Then I'm trying to develop a function placeCaretAt(position) in javascript in such a way that,

it'll place cursor at positionth character from the .text() of that div on focus event.

ie, for ex: when it is called as

$('.textA').focus(function(){ 
placeCaretAt(20); 
});

then it should place cursor in between S & O of SO users like <div class='xyz' contentEditable='true'>S|O</div>.

for getting text without tags from contentEditable I use .text() of Jquery.

Can experts here please help me? any help/advice/suggestions are welcome :). Thanks in advance.

I had already seen:

How to set caret(cursor) position in contenteditable element (div)?

jquery Setting cursor position in contenteditable div

Set cursor position on contentEditable <div>

But they were of no use for me. Bad Luck and a day :(. Please Help me experts.

Community
  • 1
  • 1
Vedant Terkar
  • 4,553
  • 8
  • 36
  • 62
  • http://stackoverflow.com/questions/16095155/javascript-contenteditable-set-cursor-caret-to-index – Tim Down Aug 06 '14 at 22:36
  • @TimDown, When I said experts, I really meant guys like you. Seriously. I've seen your every answer regarding `contentEditable` and +1'ed it :). I think you've special bond with `contentEditable`'s `caret` :D. But mate In that answer you've only handled `textNodes` my `div` contains more than that. can you please help me? – Vedant Terkar Aug 07 '14 at 05:17
  • Thanks :) Just handling text nodes is the simplest approach but has many drawbacks which I mentioned in the linked answer. I've written a more heavyweight solution for my Rangy library that handles a lot of the complexity: https://github.com/timdown/rangy/wiki/Text-Range-Module – Tim Down Aug 07 '14 at 08:27

0 Answers0