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

  • Home
  • SEARCH
  • 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 8481997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:42:40+00:00 2026-06-10T19:42:40+00:00

I have a standard content editable div: <div id="profileBody" class="customInputDiv" contentEditable=’true’ autocomplete="off"></div> Basically I

  • 0

I have a standard content editable div:

<div id="profileBody" class="customInputDiv" contentEditable='true' autocomplete="off"></div>

Basically I want to detect whereabouts the cursor is on a certain event (preferably code that works onclick, keydown or on button).

Something like this:
onkeydown – save caret position within the div, then set caret position.

This sounds slightly silly, but is required because on certain events I will be changing the content slightly and when doing the cursor position changes which is very annoying.

I hate doing questions without examples of what I’ve tried but if you read previous questions I’ve always provide what I’ve tried very detailed, just hit a dead end here!

function setCaret() {
    var el = document.getElementById("editable");
    var range = document.createRange();
    var sel = window.getSelection();
    range.setStart(el.childNodes[2], 5);
    range.collapse(true);
    sel.removeAllRanges();
    sel.addRange(range);
    el.focus();
}
​

Maybe a good way of parsing the data from window.getSelection();?

Found: http://jsfiddle.net/s5xAr/3/ But i dont want the — just want it to get the caret position using a function and set it e.g.

getCaretPOS();
//do something
setCaretPOS();

This code will set it but getting it seems to be the issue..

  • 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-10T19:42:42+00:00Added an answer on June 10, 2026 at 7:42 pm

    To get the caret position, use this function:

    function getCaretPosition(editableDiv) {
        var caretPos = 0, containerEl = null, sel, range;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.rangeCount) {
                range = sel.getRangeAt(0);
                if (range.commonAncestorContainer.parentNode == editableDiv) {
                    caretPos = range.endOffset;
                }
            }
        } else if (document.selection && document.selection.createRange) {
            range = document.selection.createRange();
            if (range.parentElement() == editableDiv) {
                var tempEl = document.createElement("span");
                editableDiv.insertBefore(tempEl, editableDiv.firstChild);
                var tempRange = range.duplicate();
                tempRange.moveToElementText(tempEl);
                tempRange.setEndPoint("EndToEnd", range);
                caretPos = tempRange.text.length;
            }
        }
        return caretPos;
    }
    

    This should work fine for most browsers (leaving IE, of course). You seem to have IE covered with you setCaret function.

    That function was from another SO answer. Have a look at the comments as well.

    Demo

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

Sidebar

Related Questions

I have the following standard markup: <body> <header><div class=wrapper>Header</div></header> <div id=create>create something</div> <div class=wrapper>Content</div>
I have a standard jquery ui accordion and I load content into it using
I have the standard content editor that uses an iFrame as the text area
I have standard basic article content type which, amongst other things, contains an image
Hy, I have created some dynamic textboxes with standard content. Does anyone know how
I have a standard meta tag like this: <meta name=desc content=test/> That I want
I have a T4 template that can output either optimized content or standard content
I have a fairly standard search query to find related content. Among other things,
I have a contentEditable div within which I need to allow the user to
I have a standard HTML formatted table, that dynamically generates the content, via Zend

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.