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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:40:21+00:00 2026-06-13T23:40:21+00:00

I am having a scenario where I need to put cursor on text area

  • 0

I am having a scenario where I need to put cursor on text area and then click on tree view node on the same page to have selected node’s text into my textarea where I placed cursor just before clicking on tree node.

I got many answers on Stack overflow including below,

Inserting text in textarea at cursor position if cursor placed else text should append at last in IE

Inserting text after cursor position in text areа

Insert text into textarea with jQuery

How to insert text at the current caret position in a textarea

Inserting text at cursor in a textarea, with Javascript

How do I insert some text where the cursor is?

FF and Chrome works fine with above solutions but IE 8 or lower version fails (didn’t check with IE9) if focus is moved to some other control.

There is below or similar implementation for IE in almost all posts:

(function ($) {
    $.fn.getCursorPosition = function () {
        var el = $(this).get(0);
        var pos = 0;
        if ('selectionStart' in el) {
            pos = el.selectionStart;
        } else if ('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength
        }
        return pos;
    }
})(jQuery);

Note : We also can use if(el.selectionStart || el.selectionStart == '0') instead of if ('selectionStart' in el) and if(document.selection) instead of if ('selection' in document)

But this will fail when focus is moved to some other control first and then executing it. In my case focus will be moved to tree nodes when user will traverse through nodes.

Is there any solution for this scenario?

I am thinking to write onkeyup and onclick on text area and save its cursor position into hidden field so when focus is moved to some other control, i will have hidden field to get cursor position of text area. I will post that here later, meanwhile if anyone has some good idea then please share.

Thank you in advance

  • 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-13T23:40:22+00:00Added an answer on June 13, 2026 at 11:40 pm

    As i mentioned above i have below code to make it working in IE as well,
    (Also thought about having only onblur instead of these 2 events but it didn’t work as focus already lost when execution comes into my code to set hidden variable)

    Below implementation It is working fine in my case.

    if ($("#myTextArea").get(0).selectionStart == undefined) {
        $("#myTextArea").click(function (e) {
            $("#hdnTextAreaPosition").val($("#myTextArea").getCursorPosition());
        });
        $("#myTextArea").keyup(function (e) {
            $("#hdnTextAreaPosition").val($("#myTextArea").getCursorPosition());
        });
    }
    

    Above events (keyup and click) are in global script and will be attached only in case of selectStart is undefined

    function getTextAreaCursorPosition() {
        if ($("#myTextArea").get(0).selectionStart == undefined) {
            return $("#hdnTextAreaPosition").val();
        }
        else {
            return $("#myTextArea").getCursorPosition();
        }
    }
    
    
    function insertToMyTextArea(textToInsert) {
        $("#myTextArea").focus();
        var cursorPosition = getTextAreaCursorPosition();
        var myContent = $("#myTextArea").val();
        $("#myTextArea").val(myContent.substring(0, cursorPosition) + textToInsert + myContent.substring(cursorPosition));
    }
    

    insertToMyTextArea is the main function i am calling on click of tree node.

    Please share your views if any alternative solution is available instead of having events.

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

Sidebar

Related Questions

I have one scenario where I need to select all files having aliencoders.numeric-digits like
Scenario Having already read a post on this on the same site, which didn't
I am having a scenario, in which i have multiple delegates attached to the
I'm having scenario to which a sql query need to be built. I tried
I am having a scenario where I need to call a java method which
The scenario is that i have a bulky database of around 500,000 records having
Hi people as a new web programming im having a scenario and i need
I have a scenario inwhich users of a site I am building need the
I am having some trouble with the following scenario: What I need is a
I have a strange scenario where I need to return the right-most non-zero column

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.