Home > Javascript > Javascript : Getting and Setting Caret Position in Textarea

Javascript : Getting and Setting Caret Position in Textarea

December 27th, 2005 Leave a comment Go to comments
function doGetCaretPosition (ctrl) {
	var CaretPos = 0;	// IE Support
	if (document.selection) {
	ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	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();
	}
}

Here “ctrl” is the Textarea object. This script works well with Internet Explorer, Firefox and Opera.

You can check it at Get/Set Caret in Textarea Example

Categories: Javascript Tags:
  1. May 12th, 2010 at 00:49 | #1

    kindly dont block the freakin content with the ads

  2. Haig
    May 26th, 2010 at 08:24 | #2

    Hey everyone. This conversation has really helped point me in the right direction. I was having major issues with IE document selection. I tried many solutions, and found Alex’s to be the best. However, the code is really inefficient and starts to show when you’re working with a good amount of text in a textarea. I therefore made a few enhancements to Alex’s code to prevent looping through every character from the beginning of the textarea everytime you need to get cursor/selection positions. I also added the code to return the endpoint as well as the startpoint for selection range. I tested this in on FF3.5/Ubuntu/Win, IE8/Win, Chrome 4.1/Win, Chrome 5/Ubuntu, Safari4/Win, Opera10.53/Win, I hope this helps someone out. Cheers!

    var textArea = $(‘myTextArea’);
    var getPos = function() {setTimeout(“debugOutput();”, 1);};
    var pos = 0;

    textArea.onkeydown = getPos;

    function debugOutput() {
    var sel = getCursorPosition(textArea, pos);
    pos = sel.start;
    $(‘lblStart’).innerHTML = sel.start;
    $(‘lblEnd’).innerHTML = sel.end;
    }

    function getCursorPosition(el, pos) {
    if (document.selection) { // IE…
    el.focus();
    var sel = document.selection.createRange();
    var sel2 = sel.duplicate();
    sel2.moveToElementText(el);

    if(sel.text.length > 1) {
    pos = pos – sel.text.length;
    if(pos < 0) {
    pos = 0;
    }
    }

    var caretPos = -1 + pos;
    sel2.moveStart('character', pos);

    while(sel2.inRange(sel)) {
    sel2.moveStart('character');
    caretPos++;
    }
    var selStr = sel.text.replace(/\r/g, "");
    return {start: caretPos, end: selStr.length + caretPos};
    }else if (el.selectionStart || el.selectionStart == 0) { //Most other browsers
    return {start: el.selectionStart, end: el.selectionEnd};
    }
    }

    function $(el) {
    return document.getElementById(el);
    }

  3. Simon Sanderson
    June 9th, 2010 at 21:44 | #3

    @Alex this posting works just fantastic Alex August 19th, 2007 at 03:35 #18. I wasted about 3 days trying to understand othermechanisms to get around problems (some of) associated with IE V6.
    This was the only example of code that behaved the same in both FF and IE for me :-) )
    Thanks to you Alex

  4. Lucas
    June 30th, 2010 at 06:23 | #4

    @Alex
    You are the best!

  5. Jayesh Trivedi
    September 14th, 2010 at 01:43 | #5

    This code works for me. its really gr8. Thanx a lot!!!!

  6. asdf
    October 2nd, 2010 at 19:13 | #6

    code works! but the AdSense on this page is annoying..

  7. teamwork
    October 11th, 2010 at 03:31 | #7

    Awesome!!! Tnx

  8. r
    October 15th, 2010 at 00:05 | #8

    nice

  9. December 27th, 2010 at 05:31 | #9

    Hi Vishal,
    Thank u for your solution. I was searching for a long time and was banging my hang for this problem. Your solution worked like a swift. Thank u once again.

  10. Pedro
    February 24th, 2011 at 21:11 | #10

    Thanks

Comment pages
1 ... 3 4 5 6 7 10
  1. July 8th, 2010 at 03:24 | #1