Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:05:55+00:00 2026-06-05T05:05:55+00:00

I am working with a contenteditable div that will have the option to have

  • 0

I am working with a contenteditable div that will have the option to have inline html elements such as tags ” <p> <font> <br>
” in the text flow.

At certain points I need to grab the caret position(cursor position) of contenteditable div, the caret(cursor) is after an html child element.

i am using following code in javascript for Firefox it works correctly to find caret position(cursor position) of contenteditable div, but i does not find any solution for Internet Explorer to find caret position (cursor position) as window.getSelection is undefined.

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 Internet Expolrer but i don’t think it will work for me to find caret position(cursor position) of contenteditable div.

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><b>t|wo</b></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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:05:57+00:00Added an answer on June 5, 2026 at 5:05 am

    Hi i found answer for internet explorer version 8 or below

           var cursorPosition=0;    
           if (document.selection && document.selection.createRange) {
                range = document.selection.createRange();
                if (range.parentElement() == YourEditableControl) {
                    var tmpEle = document.createElement("span");
                    YourEditableControl.insertBefore(tmpEle, YourEditableControl.firstChild);
                    var tmpRange = range.duplicate();
                    tmpRange.moveToElementText(tmpEle);
                    tmpRange.setEndPoint("EndToEnd", range);
                    cursorPosition= tmpRange.text.length;
                }
            }
    

    above code solve my problem to find current cursor position for IE8 or below version
    as window.getSelection() is undifined for IE8 or below and works fine with IE9

    so one can use document.selection a selection object and range object to get current caret or cursor position form contenteditable div or other control

    i hope this will help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to remove all div tags, but not content, which it have? (Working with
I currently have a simple <div contenteditable=true> working, but, here's my problem. Currently, the
i'm working on an app that will have a listing of the data you've
Working on a multi-tenant app where most of my models will have a tenant_id
Hii, I m using a simple HTML Text editor by providig 'contentEditable' property of
I'm working on a text editor. When the user double-clicks on a specific <div>
Working with SSRS and a SQL 2000 source, I have a stored procedure that
Can i apply css to selected text in a contenteditable div instead of using
I am having problems with getting a HTML editor working. We are using contentEditable
I'm working on a simple blog system and I'm using contenteditable so that users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.