I am not able to figure out, how to highlight a selected text in an editable div (when it contains many tags (nested) in it).
If I could get the exact position from where selection starts (index in the editable div, innerHTML), I could add a font tag over there with background color style (giving the effect of highlight). But I am not able to figure out a reliable way to get the index from where the selection starts.
My sample code is at: Sample.
"window.getSelection();" method gives me complete text of what has been selected, including nested tags (if I use innerHTML on it). Thus all I need is a reliable way to calculate the start index from where selection is starting.
Any help/reference is greatly appreciated.
Edit: Suppose my oDiv has content: <br><br><h2>I am H2, Title</h2>
And say I select H2 above, I need starting index as: 19 (counting all the tags inside oDiv). How can I get that? I need it to work for arbitrary deep nesting.