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 945687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:47:35+00:00 2026-05-15T22:47:35+00:00

I want to get the caret position inside text area (Internet Explorer only). I

  • 0

I want to get the caret position inside text area (Internet Explorer only).
I have this:

document.activeElement.focus();             
var sel2 = document.selection.createRange();
sel2.moveStart('character', -document.activeElement.value.length);
var caretPos = sel2.text.length;
alert(caretPos);

If textarea is one line only I get correct result, but if there are many lines the result is wrong because new line takes extra char that is not visible…

How can I get the correct position value (without removing \r\n and similar dirty work)?

  • 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-05-15T22:47:35+00:00Added an answer on May 15, 2026 at 10:47 pm

    I would argue that counting line breaks as two characters when calculating selection boundaries/cursor position is the correct approach. Fundamentally, those two characters \r\n are there in the text. They are present in the textarea’s value property and they are in the value that is submitted to the server.

    The only arguments I can see for counting line breaks as one character are:

    1. Consistency with other browsers
    2. IE’s TextRange methods consider the line breaks as one character

    I think neither is valid. Firstly, IE is already inconsistent with other browsers in counting line breaks as two characters rather than one. Secondly, IE’s TextRange character-based methods are a little insane anyway, causing problems wherever they’re used.

    I think it makes total sense to consider the selection/caret position as a position relative to the actual text in the textarea. This allows easy manipulation of the text.

    Here are two main functions. The first is the only textarea selection/caret position getting function I’ve seen that works correctly with all line breaks. You can find this here:
    How to get the start and end points of selection in text area?. Second, here’s a complementary setSelection function:

    function offsetToRangeCharacterMove(el, offset) {
        return offset - (el.value.slice(0, offset).split("\r\n").length - 1);
    }
    
    function setSelection(el, startOffset, endOffset) {
        var range = el.createTextRange();
        var startCharMove = offsetToRangeCharacterMove(el, startOffset);
        range.collapse(true);
        if (startOffset == endOffset) {
            range.move("character", startCharMove);
        } else {
            range.moveEnd("character", offsetToRangeCharacterMove(el, endOffset));
            range.moveStart("character", startCharMove);
        }
        range.select();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a string text, a caret position caret and then want to
I want to get text from current active line (the line where the caret
In a .NET 3.5 application, i want to get caret position in a RichTextBox
I am wondering how can I achieve this? I want to get only distinct
I have a long text field and what I want to do is that
I have problem with jCaret plugin in IE. jCaret downloads I want to get
I want to have a dialog that looks kinda like this: I thought this
Lets say I have the table below. I want to get all the friends,
I need to get an input point (caret position, window/control) that is focused. My
Trying to do this with a single query, but want to get a list

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.