JQuery extensions for text field cursor manipulation

  • 2020-03-30 02:21:15
  • OfStack

The functions and usage of this extension for text field:
1. Get cursor position: $(elem). IGetFieldPos ();
2. Set the cursor position: $(elem). ISelectField (start);
3. Select the character in the specified position: $(elem). ISelectField (start,end);
4. Select the specified character: $(elem). ISelectStr (STR);
5. Insert string after cursor: $(elem). IAdd (STR);
6. Delete n characters before (-n) or after (n) of the cursor: $(elem). IDel (n);

JQuery extension code:


;(function($){
    
    $.fn.extend({
        
        iGetFieldPos:function(){
            var field=this.get(0);
            if(document.selection){
                //IE
                $(this).focus();
                var sel=document.selection;
                var range=sel.createRange();
                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;
        },
        
        iSelectField:function(start,end){
            var field=this.get(0);
            //If end is not defined, set the cursor position
            if(arguments[1]==undefined){
                end=start;
            }
            if(document.selection){
                //IE
                var range = field.createTextRange();
                range.moveEnd('character',-$(this).val().length);
                range.moveEnd('character',end);
                range.moveStart('character',start);
                range.select();
            }else{
                //The IE
                field.setSelectionRange(start,end);
                $(this).focus();
            }
        },
        
        iSelectStr:function(str){
            var field=this.get(0);
            var i=$(this).val().indexOf(str);
            i != -1 ? $(this).iSelectField(i,i+str.length) : false;
        },
        
        iAddField:function(str){
            var field=this.get(0);
            var v=$(this).val();
            var len=$(this).val().length;
            if(document.selection){
                //IE
                $(this).focus()
                document.selection.createRange().text=str;
            }else{
                //The IE
                var selPos=field.selectionStart;
                $(this).val($(this).val().slice(0,field.selectionStart)+str+$(this).val().slice(field.selectionStart,len));
                this.iSelectField(selPos+str.length);
            };
        },
        
        iDelField:function(n){
            var field=this.get(0);
            var pos=$(this).iGetFieldPos();
            var v=$(this).val();
            //If it is greater than 0, it will delete the back. If it is less than 0, it will delete the front
            $(this).val(n>0 ? v.slice(0,pos-n)+v.slice(pos) : v.slice(0,pos)+v.slice(pos-n));
            $(this).iSelectField(pos-(n<0 ? 0 : n));
        }
    });
})(jQuery);

Load it into the extension code and call it based on the method name in the extension.


Related articles: