Full power jQuery Carat plugin





1
Date Submitted Thu. Sep. 17th, 2009 10:10 PM
Revision 1 of 1
Scripter Cloudgen
Tags carat | jquery | plugin | textarea | textbox
Comments 0 comments
jCaret is a full-power caret Plugin.

It provides basic selection detection in textbox and textarea giving the following information
The start postion of caret/cursor
The end postion of caret/cursor
The selected text
It enable programmer highlight part of the text in textbox or textarea by:
providing the text directly
providing the start and end position
providing the regular expression for selecting text
It provides the replace() method, which is useful in providing input filter of a textbox or textarea.
See online demostration
jCaret plugi

<script type="text/javascript">
(function($){
        $.fn.caret=function(options,opt2){
                var start,end,t=this[0];
                if(typeof options==="object" && typeof options.start==="number" && typeof options.end==="number") {
                        start=options.start;
                        end=options.end;
                } else if(typeof options==="number" && typeof opt2==="number"){
                        start=options;
                        end=opt2;
                } else if(typeof options==="string"){
                        if((start=t.value.indexOf(options))>-1) end=start+options.length-1;
                        else start=null;
                } else if(Object.prototype.toString.call(options)==="[object RegExp]"){
                        var re=options.exec(t.value);
                        if(re != null) {
                                start=re.index;
                                end=start+re[0].length-1;
                        }
                }
                if(typeof start!="undefined"){
                        end++;
                        if($.browser.msie){
                                var selRange = this[0].createTextRange();
                                selRange.collapse(true);
                                selRange.moveStart('character', start);
                                selRange.moveEnd('character', end-start);
                                selRange.select();
                        } else {
                                this[0].selectionStart=start;
                                this[0].selectionEnd=end;
                        }
                        this[0].focus();
                        return this
                } else {
                        if($.browser.msie){
                                var val = this.val();
                                var range = document.selection.createRange().duplicate();
                                range.moveEnd("character", val.length)
                                var s = (range.text == "" ? val.length : val.lastIndexOf(range.text));
                                range = document.selection.createRange().duplicate();
                                range.moveStart("character", -val.length);
                                var e = range.text.length;                       
                        } else {
                                var s=t.selectionStart,
                                        e=t.selectionEnd;
                        }
                        var te=t.value.substring(s,e);
                        return {start:s,end:e,text:te,replace:function(st){
                                return t.value.substring(0,s)+st+t.value.substring(e,t.value.length-1)
                        }}
                }
                return this;
        }
})(jQuery);
</script>
 

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Scripter Cloudgen

Votes Down