Set keyboard caret position in html textbox Set keyboard caret position in html textbox javascript javascript

Set keyboard caret position in html textbox


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);


The link in the answer is broken, this one should work (all credits go to blog.vishalon.net):

http://snipplr.com/view/5144/getset-cursor-in-html-textarea/

In case the code gets lost again, here are the two main functions:

function doGetCaretPosition(ctrl){ var CaretPos = 0; if (ctrl.selectionStart || ctrl.selectionStart == 0) {// Standard.  CaretPos = ctrl.selectionStart; } else if (document.selection) {// Legacy IE  ctrl.focus ();  var Sel = document.selection.createRange ();  Sel.moveStart ('character', -ctrl.value.length);  CaretPos = Sel.text.length; } return (CaretPos);}function setCaretPosition(ctrl,pos){ if (ctrl.setSelectionRange) {  ctrl.focus();  ctrl.setSelectionRange(pos,pos); } else if (ctrl.createTextRange) {  var range = ctrl.createTextRange();  range.collapse(true);  range.moveEnd('character', pos);  range.moveStart('character', pos);  range.select(); }}


Since I actually really needed this solution, and the typical baseline solution (focus the input - then set the value equal to itself) doesn't work cross-browser, I spent some time tweaking and editing everything to get it working. Building upon @kd7's code here's what I've come up with.

Enjoy! Works in IE6+, Firefox, Chrome, Safari, Opera

Cross-browser caret positioning technique (example: moving the cursor to the END)

// ** USEAGE ** (returns a boolean true/false if it worked or not)// Parameters ( Id_of_element, caretPosition_you_want)setCaretPosition('IDHERE', 10); // example

The meat and potatoes is basically @kd7's setCaretPosition, with the biggest tweak being if (el.selectionStart || el.selectionStart === 0), in firefox the selectionStart is starting at 0, which in boolean of course is turning to False, so it was breaking there.

In chrome the biggest issue was that just giving it .focus() wasn't enough (it kept selecting ALL of the text!) Hence, we set the value of itself, to itself el.value = el.value; before calling our function, and now it has a grasp & position with the input to use selectionStart.

function setCaretPosition(elemId, caretPos) {    var el = document.getElementById(elemId);    el.value = el.value;    // ^ this is used to not only get "focus", but    // to make sure we don't have it everything -selected-    // (it causes an issue in chrome, and having it doesn't hurt any other browser)    if (el !== null) {        if (el.createTextRange) {            var range = el.createTextRange();            range.move('character', caretPos);            range.select();            return true;        }        else {            // (el.selectionStart === 0 added for Firefox bug)            if (el.selectionStart || el.selectionStart === 0) {                el.focus();                el.setSelectionRange(caretPos, caretPos);                return true;            }            else  { // fail city, fortunately this never happens (as far as I've tested) :)                el.focus();                return false;            }        }    }}