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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:32:15+00:00 2026-06-13T09:32:15+00:00

Is there a total solution for getting a caret position and/or selection inside every

  • 0

Is there a total solution for getting a caret position and/or selection inside every browser from different elements. I’m looking for a solution witch I can execute like
mGetCaretPosition(iControl) witch will return the caret position inside it`s element.

I have tried a lot of functions:

  • selection (window/document) [document=IE, window=Opera]
  • getSelection (window/document) [document=Firefox, document=Chrome, document=Safari]
  • selectionStart (input/textarea) [All]
  • craeteRange (selection)
  • createTextRange (selection)

Calling a method like document.selection.createRange().text doesn’t return a caret position because it doesn’t have a selection. When setting tRange.moveStart(‘character’, -X) the X isn’t a known value. When you use this inside a div and the caret is in the middle it takes the code before the div.

  • 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-13T09:32:16+00:00Added an answer on June 13, 2026 at 9:32 am

    I have build this today. It`s a combination of youre response alex and al the other results inside google. I have tested it inside the browsers IE9, Chrome, Opera, Safari and Firefox on the PC and also on a HTC Sensation with Android with the default browser, Firefox, Chrome and Opera.

    Only the Opera on the mobile device did have some troubles.

    My solution:

    // Control
    var BSControl = function(iControl)
    {
        // Variable
        var tControl = (typeof iControl == 'string' ? document.getElementById(iControl) : iControl);
    
        // Get Caret
        this.mGetCaret = function()
        {
            // Resultaat aanmaken
            var tResult = -1;
    
            // SelectionStart
            // *) Input & Textarea
            if(tResult == -1 && (tControl.selectionStart || tControl.selectionStart == '0'))
            {
                tResult = tControl.selectionStart;
            }
    
            // ContentWindow.GetSelection
            // *) IFrame
            if(tResult == -1 && (tControl.contentWindow && tControl.contentWindow.getSelection))
            {
                var tRange= tControl.contentWindow.getSelection().getRangeAt(0); 
                tResult = tRange.startOffset;
            }
    
            // GetSelection
            // *) Div
            if(tResult == -1 && (window.getSelection))
            {
                var tRange= window.getSelection().getRangeAt(0); 
                tResult = tRange.startOffset;
            }
    
            // Resultaat teruggeven
            return tResult;
        }
    
        // Set Caret
        this.mSetCaret = function(iPosition)
        {
            // SelectionStart
            // *) Input & Textarea
            if(tControl.selectionStart || tControl.selectionStart == '0')
            {
                tControl.selectionStart = iPosition;
                tControl.selectionEnd = iPosition;
                return;
            }
    
            // ContentWindow.GetSelection
            // *) IFrame
            if(tControl.contentWindow && tControl.contentWindow.getSelection)
            {
                var tRange = tControl.contentDocument.createRange();
                tRange.setStart(tControl.contentDocument.body.firstChild, iPosition);
                tRange.setEnd(tControl.contentDocument.body.firstChild, iPosition);
    
                var tSelection = tControl.contentWindow.getSelection();
                tSelection.removeAllRanges();
                tSelection.addRange(tRange);
    
                return;
            }
    
            // GetSelection
            // *) Div
            if(window.getSelection)
            {
                var tSelection = window.getSelection();
                var tRange= tSelection.getRangeAt(0); 
    
                tRange.setStart(tControl.firstChild, iPosition);
                tRange.setEnd(tControl.firstChild, iPosition);
    
                tSelection.removeAllRanges();
                tSelection.addRange(tRange);
    
                return;
            }
        }
    
        // Get Selection
        this.mGetSelection = function()
        {
            // Resultaat aanmaken
            var tResult = null;
    
            // SelectionStart
            // *) Input & Textarea
            if(tResult == null && (tControl.selectionStart || tControl.selectionStart == '0'))
            {
                tResult = this.mGet().substring(tControl.selectionStart, tControl.selectionEnd);
            }
    
            // ContentWindow.GetSelection
            // *) IFrame
            if(tResult == null && (tControl.contentWindow && tControl.contentWindow.getSelection))
            {
                var tSelection = tControl.contentWindow.getSelection() 
                tResult = tSelection.toString();
            }
    
            // GetSelection
            // *) Div
            if(tResult == null && (window.getSelection))
            {
                var tSelection = window.getSelection() 
                tResult = tSelection.toString();
            }
    
            // Resultaat teruggeven
            return tResult;
        }
    
        // Set Selection
        this.mSetSelection = function(iFrom, iUntil)
        {
            // SelectionStart
            // *) Input & Textarea
            if(tControl.selectionStart || tControl.selectionStart == '0')
            {
                tControl.selectionStart = iFrom;
                tControl.selectionEnd = iUntil;
                return;
            }
    
            // ContentWindow.GetSelection
            // *) IFrame
            if(tControl.contentWindow && tControl.contentWindow.getSelection)
            {
                var tRange = tControl.contentDocument.createRange();
                tRange.setStart(tControl.contentDocument.body.firstChild, iFrom);
                tRange.setEnd(tControl.contentDocument.body.firstChild, iUntil);
    
                var tSelection = tControl.contentWindow.getSelection();
                tSelection.removeAllRanges();
                tSelection.addRange(tRange);
    
                return;
            }
    
            // GetSelection
            // *) Div
            if(window.getSelection)
            {
                var tSelection = window.getSelection();
                var tRange= tSelection.getRangeAt(0); 
    
                tRange.setStart(tControl.firstChild, iFrom);
                tRange.setEnd(tControl.firstChild, iUntil);
    
                tSelection.removeAllRanges();
                tSelection.addRange(tRange);
    
                return;
            }
        }
    
        // Set
        this.mSet = function(iValue)
        {
            // Afhankelijk van aanwezige property waarde instellen
            if('value' in tControl)
            {
                tControl.value = iValue;
            }else if('innerText' in tControl)
            {
                tControl.innerText = iValue;
            }else if('textContent' in tControl)
            {
                tControl.textContent = iValue;
            }else if('innerHTML' in tControl)
            {
                tControl.innerHTML = iValue;
            }
        }
    
        // Get
        this.mGet = function()
        {
            // Resultaat aanmaken
            var tResult = null;
    
            // Afhankelijk van aanwezige property waarde instellen
            if('value' in tControl)
            {
                tResult = tControl.value;
            }else if('innerText' in tControl)
            {
                tResult = tControl.innerText;
            }else if('textContent' in tControl)
            {
                tResult = tControl.textContent;
            }else if('innerHTML' in tControl)
            {
                tResult = tControl.innerHTML;
            }
    
            // Resultaat teruggeven
            return tResult;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to limit the total selection of rows in a
is there a commend to delete project from svn with all its revisions(total cleanup)
There is a way to get the total memory PHP is using ( memory_get_usage()
Is there a way to add a daily total to a graph made with
Is there a way to get a total count of all webs in a
Is there any way to get total price of all products by category and
Is there any way possible to find out the total number of sent emails
Is there a way to center text in the cell here? grid.Column(TotalSum, Total Sum,
And if it does, is there an easy way to get the total time
I'm looking for advice here on a smart solution to my problem. I'm writing

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.