0

I have a contenteditable div. I want to colorize specific text (which is like A4, D12 etc) while typing.

So on each keypress i check the content of div, find out the tokens(A4,D11 etc) and wrap them into span.

So abcd+A6 will get converted as abcd+<span color=#some-color>A6</span>

Now i want to know the current caret position. Let say cursor is after A inside the span,

but when i do

sel=window.getSelection()
pos = sel.anchorOffset

i am getting pos=1 that is the position of the caret in the span element But i want the caret positon relative to the div element (it should be pos=6 in this case)

Any help would be appreciated.

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55

1 Answers1

0

Try this:

sel = window.getSelection();
pos = sel.focusOffset;

You can see more here

Ringo
  • 3,795
  • 3
  • 22
  • 37