sergzach
sergzach

Reputation: 6764

HTML Textarea: is it possible to change cursor position?

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

Upvotes: 0

Views: 3343

Answers (1)

Barkermn01
Barkermn01

Reputation: 6842

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

Upvotes: 2

Related Questions