Click here to Skip to main content
12,954,764 members (67,882 online)
Rate this:
 
Please Sign up or sign in to vote.
I am working with a contenteditable div that will have the option to have inline html elements such as tags "<p > <font>" in the text flow.

At certain points I need to grab the caret position, the caret is after an html child element.

i am using following code for FF but not find any solution for Internet Explorer

 function  getCaretPosition() {
if (window.getSelection && window.getSelection().getRangeAt) {
                var range = window.getSelection().getRangeAt(0);
                var selectedObj = window.getSelection();
                var rangeCount = 0;
                var childNodes = selectedObj.anchorNode.parentNode.childNodes;
                for (var i = 0; i < childNodes.length; i++) {
                    if (childNodes[i] == selectedObj.anchorNode) {
                        break;
                    }
                    if (childNodes[i].outerHTML)
                        rangeCount += childNodes[i].outerHTML.length;
                    else if (childNodes[i].nodeType == 3) {
                        rangeCount += childNodes[i].textContent.length;                       
                    }
                }
                return range.startOffset + rangeCount;
            }
return -1;
}


i found that document.selection; works on InternetExpolrer but i don't think it will work for me

can any one have any work around for me.

in below example my cursor position is at between 't' and 'w' in <p>two</p>
<div contenteditable="true"><p>one<br><p>t|wo</p></p></div>
i suppose to need number 14 in above example as i need to perform some operation at that point i am using this contenteditable div as RichTextbox with my custom style apply to it
Posted 30-May-12 20:53pm
parth8911.3K
Updated 4-Jun-12 2:04am
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100