0

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

sergzach
  • 6,242
  • 6
  • 42
  • 77
  • 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,070
  • 7
  • 48
  • 74
Barkermn01
  • 6,560
  • 32
  • 80
1

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

Community
  • 1
  • 1
jams
  • 20,714
  • 26
  • 72
  • 94
  • And this one http://stackoverflow.com/questions/3286595/update-textarea-value-but-keep-cursor-position – Ishmael May 26 '11 at 15:58