The cursor focused position is at the front
jquery focused text box-Script Home
jquery extended text box focus method
In different browsers, a text box, if only If you set focus() directly to the text box, the cursor focus position may be at the front. The following code extends jquery with a textFocus method to focus the text box and put the cursor at the end, using $("input").textFocus(). You can also pass in a numeric parameter to set the position of the cursor focus. For example, $("input").textFocus(2), the cursor is after the second character.
(function($){
$.fn .textFocus=function(v){
var range,len,v=v===undefined?0:parseInt(v);
this.each(function(){
if($.browser .msie){
range=this.createTextRange(); //Text box creation range
v===0?range.collapse(false):range.move("character",v); // Range folding
range.select(); //Select
}else{
len=this.value.length;
v===0?this.setSelectionRange(len,len):this .setSelectionRange(v,v); //dom sets the selection directly, and then focus
}
this.focus();
});
return this;
}
} )(jQuery)