The functions and usage of this extension for text fields:
1. Get the cursor position: $(elem).iGetFieldPos();
2. Set the cursor position: $(elem).iSelectField( start);
3. Select the characters in the specified position: $(elem).iSelectField(start,end);
4. Select the specified characters: $(elem).iSelectStr(str);
5. Insert a string after the cursor: $(elem).iAdd(str);
6. Delete n characters before (-n) or after (n) the cursor: $(elem).iDel(n) ;
jQuery extension code:
;(function($){
/*
* jQuery extension for text field cursor operations (select, add, delete, take)
*/
$.fn. extend({
/* if(document. selection){
. );
var dupRange=range .duplicate();
dupRange.moveToElementText(field);
dupRange.setEndPoint('EndToEnd',range);
field.selectionStart=dupRange.text.length- range.text.length;
field.selectionEnd=field.selectionStart range.text.length;
}
return field.selectionStart;
},
/*
* Select the characters in the specified position || Set the cursor position
but After the start character)
*/
iSelectField:function(start,end){
var field=this.get(0);
//end is not defined, then the cursor position is set
if(arguments[1]==undefined){
end=start;
}
if(document.selection){
//IE
var range = field.createTextRange() ;
range.moveEnd('character',-$(this).val().length); start);
range.select();
}else{
//Non-IE
field.setSelectionRange(start,end);
$(this).focus();
=$( this).val().indexOf(str);
i != -1 ? $(this).iSelectField(i,i str.length) : false;
},
/*
* Insert string
after the cursor */ ;
var len=$(this).val().length;
if(document.selection){
document. selection.createRange().text=str;
$(this).val( ).slice(0,field.selectionStart) str $(this).val().slice(field.selectionStart,len));
this.iSelectField(selPos str.length);
};
(0);
var pos=$(this).iGetFieldPos();
var v=$(this).val();
$(this).val(n>0 ? v.slice(0,pos-n) v.slice(pos) : v.slice(0,pos) v.slice(pos-n));
Load it in the extension code, and then call it according to the method name in the extension.