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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:03:21+00:00 2026-05-11T06:03:21+00:00

Does anybody know how to move the keyboard caret in a textbox to a

  • 0

Does anybody know how to move the keyboard caret in a textbox to a particular position?

For example, if a text-box (e.g. input element, not text-area) has 50 characters in it and I want to position the caret before character 20, how would I go about it?

This is in differentiation from this question: jQuery Set Cursor Position in Text Area , which requires jQuery.

  • 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. 2026-05-11T06:03:21+00:00Added an answer on May 11, 2026 at 6:03 am

    Excerpted from Josh Stodola’s Setting keyboard caret Position in a Textbox or TextArea with Javascript

    A generic function that will allow you to insert the caret at any position of a textbox or textarea that you wish:

    function setCaretPosition(elemId, caretPos) {     var elem = document.getElementById(elemId);      if(elem != null) {         if(elem.createTextRange) {             var range = elem.createTextRange();             range.move('character', caretPos);             range.select();         }         else {             if(elem.selectionStart) {                 elem.focus();                 elem.setSelectionRange(caretPos, caretPos);             }             else                 elem.focus();         }     } } 

    The first expected parameter is the ID of the element you wish to insert the keyboard caret on. If the element is unable to be found, nothing will happen (obviously). The second parameter is the caret positon index. Zero will put the keyboard caret at the beginning. If you pass a number larger than the number of characters in the elements value, it will put the keyboard caret at the end.

    Tested on IE6 and up, Firefox 2, Opera 8, Netscape 9, SeaMonkey, and Safari. Unfortunately on Safari it does not work in combination with the onfocus event).

    An example of using the above function to force the keyboard caret to jump to the end of all textareas on the page when they receive focus:

    function addLoadEvent(func) {     if(typeof window.onload != 'function') {         window.onload = func;     }     else {         if(func) {             var oldLoad = window.onload;              window.onload = function() {                 if(oldLoad)                         oldLoad();                  func();             }         }     } }  // The setCaretPosition function belongs right here!  function setTextAreasOnFocus() { /***  * This function will force the keyboard caret to be positioned  * at the end of all textareas when they receive focus.  */     var textAreas = document.getElementsByTagName('textarea');      for(var i = 0; i < textAreas.length; i++) {         textAreas[i].onfocus = function() {             setCaretPosition(this.id, this.value.length);         }     }      textAreas = null; }  addLoadEvent(setTextAreasOnFocus); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know how to programmatically disable the keyboard individual letter pop-up? (This blue
Does anybody know why my getAltitude in the following always returns 0? package com.example.helloandroid;
http://jsfiddle.net/hydroplane/r4Kn6/ I need to move that 'reblog' text out of the screen. Does anybody
Does anybody know why this function, when passed an invalid date (e.g. timestamp) to
Does anybody know if the CUDA library 'Thrust' can generate random numbers on the
does anybody know how to insert code into lyx? just like what is available
Does anybody know a good place to learn how to use the debugger in
Does anybody know how I can enable line numbers for Ruby (and Ruby on
Does anybody know a Perl library that can parse XML documents and enables me
does anybody know of an extension of shouldjs or some other assertion library which

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.