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

The Archive Base Latest Questions

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

How do you set the cursor position in a text field using jQuery? I’ve

  • 0

How do you set the cursor position in a text field using jQuery? I’ve got a text field with content, and I want the users cursor to be positioned at a certain offset when they focus on the field. The code should look kind of like this:

$('#input').focus(function() {   $(this).setCursorPosition(4); }); 

What would the implementation of that setCursorPosition function look like? If you had a text field with the content abcdefg, this call would result in the cursor being positioned as follows: abcd**|**efg.

Java has a similar function, setCaretPosition. Does a similar method exist for javascript?

Update: I modified CMS’s code to work with jQuery as follows:

new function($) {   $.fn.setCursorPosition = function(pos) {     if (this.setSelectionRange) {       this.setSelectionRange(pos, pos);     } else if (this.createTextRange) {       var range = this.createTextRange();       range.collapse(true);       if(pos < 0) {         pos = $(this).val().length + pos;       }       range.moveEnd('character', pos);       range.moveStart('character', pos);       range.select();     }   } }(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-11T03:45:22+00:00Added an answer on May 11, 2026 at 3:45 am

    I have two functions:

    function setSelectionRange(input, selectionStart, selectionEnd) {   if (input.setSelectionRange) {     input.focus();     input.setSelectionRange(selectionStart, selectionEnd);   }   else if (input.createTextRange) {     var range = input.createTextRange();     range.collapse(true);     range.moveEnd('character', selectionEnd);     range.moveStart('character', selectionStart);     range.select();   } }  function setCaretToPos (input, pos) {   setSelectionRange(input, pos, pos); } 

    Then you can use setCaretToPos like this:

    setCaretToPos(document.getElementById('YOURINPUT'), 4); 

    Live example with both a textarea and an input, showing use from jQuery:

    function setSelectionRange(input, selectionStart, selectionEnd) {   if (input.setSelectionRange) {     input.focus();     input.setSelectionRange(selectionStart, selectionEnd);   } else if (input.createTextRange) {     var range = input.createTextRange();     range.collapse(true);     range.moveEnd('character', selectionEnd);     range.moveStart('character', selectionStart);     range.select();   } }  function setCaretToPos(input, pos) {   setSelectionRange(input, pos, pos); }  $('#set-textarea').click(function() {   setCaretToPos($('#the-textarea')[0], 10) }); $('#set-input').click(function() {   setCaretToPos($('#the-input')[0], 10); });
    <textarea id='the-textarea' cols='40' rows='4'>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea> <br><input type='button' id='set-textarea' value='Set in textarea'> <br><input id='the-input' type='text' size='40' value='Lorem ipsum dolor sit amet, consectetur adipiscing elit'> <br><input type='button' id='set-input' value='Set in input'> <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>

    As of 2016, tested and working on Chrome, Firefox, IE11, even IE8 (see that last here; Stack Snippets don’t support IE8).

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to ban the modifying operations at compile… May 12, 2026 at 12:07 am
  • Editorial Team
    Editorial Team added an answer Repeaters fit nicely into the code-behind approach. I spent many… May 12, 2026 at 12:07 am
  • Editorial Team
    Editorial Team added an answer You need to use an XmlNamespaceManager because the Template element… May 12, 2026 at 12:07 am

Related Questions

How do you set the cursor position in a text field using jQuery? I've
Problem I am using jQuery UI dialog to show a dialog box with some
So my Vista machine is set to turn off it's monitors after 10 minutes.
I have a java program that runs a bunch of queries against an sql
Well, as the title says, how can I set an image or anything else

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.