View Full Version : selection start/end, replace text

11-28-2012, 02:56 PM
I have div with contentEditable ="true"
I can read selection.

How to replace exact selection If I have forinstance divs innerHTML like(can't do it via regex):

foo billgates foo

How to replace second foo ?

is this http://stackoverflow.com/questions/4811822/get-a-ranges-start-and-end-offsets-relative-to-its-parent-container realy so complicated,and as said there can't be guarantied it is cross browser ?

rnd me
11-28-2012, 07:23 PM
This seems simple and seems to actually work :

var whole=getSelection().getRangeAt().commonAncestorContainer;


i am not 100% sure how you are doing the replacements, but this code opens the door to reliable string processing.

11-28-2012, 08:20 PM
Why can't you do it via RegEx?