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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:30:02+00:00 2026-06-05T18:30:02+00:00

I have found many great answers to this from a year ago (when it

  • 0

I have found many great answers to this from a year ago (when it still worked on our site as well), but it seems like firefox no longer works as expected when setting the cursor position or selection in textareas and inputs.

What I would like to do is focus on a textarea and place the cursor at the beginning. All of these fiddles work fine in Chrome and IE, but fail in FF. My preference is for a jQuery solution, but I’m not offended by simple js.

Fiddles (none work in FF 11 or 12):

$.fn solution

(function($) {
    $.fn.caret = function(pos) {
        var target = this[0];
        if (arguments.length == 0) { //get
            if (target.selectionStart) { //DOM
                var pos = target.selectionStart;
                return pos > 0 ? pos : 0;
            }
            else if (target.createTextRange) { //IE
                target.focus();
                var range = document.selection.createRange();
                if (range == null) return '0';
                var re = target.createTextRange();
                var rc = re.duplicate();
                re.moveToBookmark(range.getBookmark());
                rc.setEndPoint('EndToStart', re);
                return rc.text.length;
            }
            else return 0;
        }

        //set
        var pos_start = pos;
        var pos_end = pos;

        if (arguments.length > 1) {
            pos_end = arguments[1];
        }

        if (target.setSelectionRange) //DOM
        target.setSelectionRange(pos_start, pos_end);
        else if (target.createTextRange) { //IE
            var range = target.createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos_end);
            range.moveStart('character', pos_start);
            range.select();
        }
    }
})(jQuery)

$('#input_tag').caret(1, 9);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<input type="text" id="input_tag" value="hello beautiful world" />

prototype solution

(function() {
    if (!HTMLInputElement.prototype.setSelectionRange) {
        HTMLInputElement.prototype.setSelectionRange = function(start, end) {
            if (this.createTextRange) {
                var range = this.createTextRange();
                this.collapse(true);
                this.moveEnd('character', start);
                this.moveStart('character', end);
                this.select();
            }
        }
    }
})();
document.getElementById("input_tag").setSelectionRange(6, 15);
<input type="text" id="input_tag" value="hello beautiful world" />
  • Another $.fn solution (This one curiously works on page load, but not if you move the cursor and click "Run")

This is similar to this question, but I can’t comment on any of the answers and they are all older.

Please help. Bonus points if I am missing something simple. Over half our users are on FF and I am out of ideas.

  • 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-05T18:30:04+00:00Added an answer on June 5, 2026 at 6:30 pm

    Here’s the answer I finally found for anyone else running into this.

    It used to be the case that animated CSS:visibility would just flip at the end of the timer. In my instance, however, I found that the activeElement and cursor location would not move at the same time. Because of this, things like setSelection don’t work as expected.

    CSS transitions lists visibility as a supported property, but save yourself a headache and do not use it on elements that need focus or have similar handlers attached.

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

Sidebar

Related Questions

I have found many people with simliar issues but no soultions...basically I have two
I have read many topics about cut/copy to clipboard, but haven't found the answer
I've looked this question up, but I don't see too many answers, and obviously
I have looked throughout the greatest-n-per-group tag and found great information but nothing that
I just joined StackOverflow after having found many great answers here in the past.
I have one question. I made quick search on the site, but don't found
I have found many similar posts and even tried to find out how to
I am relatively new to assembly language. I have found so many tutorials that
i found many related questions here. I have a simple java program . it
I have refereed old questions and found that people face many issues after installing

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.