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
2009-12-08 08:48:19
I use a mask edit textbox...
Pandiya Chendur
2009-12-08 08:50:35
Mark the textbox will be empty is there any solution
Pandiya Chendur
2009-12-08 08:58:25
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
2009-12-08 09:02:50
+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
2009-12-08 14:40:27
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
2009-12-09 09:07:23