0

Is it possible to change cursor position in textarea element using Javascript code?

sergzach
  • 6,578
  • 7
  • 46
  • 84
  • Possible duplicate of http://stackoverflow.com/questions/5140637/javascript-how-to-get-line-col-caret-position-in-textarea – n00dle May 26 '11 at 15:56

2 Answers2

2

YES

function SetCursorPosition(pos)
{
    // HERE txt is the text field name
    var obj=document.getElementById('<%= txt.ClientID %><%= txt.ClientID %>');

    //FOR IE
    if(obj.setSelectionRange)
    {
        obj.focus();
        obj.setSelectionRange(pos,pos);
    }

    // For Firefox
    else if (obj.createTextRange)
    {
        var range = obj.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

FROM :: http://shawpnendu.blogspot.com/2009/03/javascript-how-to-setget-cursor.html

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
Barkermn01
  • 6,781
  • 33
  • 83
1

This post may help you Caret position in textarea, in characters from the start

Community
  • 1
  • 1
Amit
  • 21,570
  • 27
  • 74
  • 94
  • And this one http://stackoverflow.com/questions/3286595/update-textarea-value-but-keep-cursor-position – Ishmael May 26 '11 at 15:58