Here are some code snippets and examples of how to use jQuery & HTML5 to set cursor Input Focus and Cursor Positions which are common actions with login forms and such. Comments, improvements and suggestions welcomed.


jQuery Input Focus

Simply call the focus() function to set focus to an input.

//set focus on input

HTML5 Input Focus

Awesome feature provided by HTML5… Couldn’t find this on but I’ve tested it’s working in Chrome & Firefox but not in IE9 or below.

<input type="text" name="myInput" autofocus />

jQuery Set Cursor Position

jQuery function to set the cursor position to a specfic character position within an input field.

$.fn.setCursorPosition = function(pos) {
  this.each(function(index, elem) {
    if (elem.setSelectionRange) {
      elem.setSelectionRange(pos, pos);
    } else if (elem.createTextRange) {
      var range = elem.createTextRange();
      range.moveEnd('character', pos);
      range.moveStart('character', pos);;
  return this;

Example Usage

Sets cursor position after first character.


jQuery Set Cursor Position

jQuery function to auto select text (specific number of characters) within an input field.

$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.moveEnd('character', end);
            range.moveStart('character', start);

Example Usage

Selects the first 5 characters in the input.


Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

Free Guide:

How to Choose the Right Charting Library for Your Application

How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.

  • Pingback: Tweet parade (no.08 Feb 2013) | gonzoblog()

  • Serdar Kuzucu

    Thank you! Useful topic and codes :)

  • Valerij

    Nice & Easy – that I was searched for

  • Yasin

    Thank you, it’s working great.

  • Beach

    This article is useful. I do have one question though.

    Why in the selectRange function is this.each() returned but in the setCursorPosition function this is returned? It is my understanding that returning this.each() allows for function chaining…Was function chaining for setCursorPosition undesired, or was there another reason/benefit to returning this instead of this.each()?

    • Stavros Ioannidis

      “this” is the jQuery object upon which we “work”. For chaining reasons “this.each()” returns the jQuery object upon which we “work”… So they are the same thing. you can see for yourself by doing console.log(this == this.each(function( ){ }))

  • freddy

    Cool i prefer using HTML5, is taht all browser support HTML5 ?

Ending Soon
Free SitePoint Premium

Get one free year of unlimited book and course downloads on SitePoint Premium!