views:

444

answers:

2

Hai Guys, I want to set cursor at a position of length 14 on a textbox which will not have a value.. Iknow initially cursor will be at 0 i want it to be at 14

A: 

$("#textbox").selectionStart=14 might works for Firefox, Opera, Chrome, but not sure for IE

PS: There should be length 14 > characters already in textbox to work properly.

S.Mark
I use a mask edit textbox...
Pandiya Chendur
Mark the textbox will be empty is there any solution
Pandiya Chendur
Yeah, Its for input box and textarea, If yours is Rich Text Box, I am not sure with it then. sorry about that.
S.Mark
+2  A: 

IE use different approach at setting cursor position than Firefox,Opera and Chrome. It's better to make a helper function, which will do it for you. I use this one for own needs.

function setCursor(node,pos){

    var node = (typeof node == "string" || node instanceof String) ? document.getElementById(node) : node;

    if(!node){
        return false;
    }else if(node.createTextRange){
        var textRange = node.createTextRange();
        textRange.collapse(true);
        textRange.moveEnd(pos);
        textRange.moveStart(pos);
        textRange.select();
        return true;
    }else if(node.setSelectionRange){
        node.setSelectionRange(pos,pos);
        return true;
    }

    return false;
}

Last thing, is to call it from your onfocus handler.

Goodluck

nemisj
Hai What is node is it textboxId
Pandiya Chendur
node is the DOM instance, but if you need to work with id's, you can add extra "codeline" which will fetch DOM instance for you.
nemisj