(function($) {
  $.fn.caret = function(pos) {
    var target = this[0];
    //get
    if (arguments.length == 0) {
      if (target.selectionStart) { //HTML5
        var pos = target.selectionStart;
        return pos > 0 ? pos : 0;
      }
      else if (target.createTextRange) { //IE<9
        target.focus();
        var range = document.selection.createRange();
        if (range == null)
            return '0';
        var re = target.createTextRange();
        var rc = re.duplicate();
        re.moveToBookmark(range.getBookmark());
        rc.setEndPoint('EndToStart', re);
        return rc.text.length;
      }
      else return 0; //not supported
    }
    //set
    if (target.setSelectionRange) //HTML5
      target.setSelectionRange(pos, pos);
    else if (target.createTextRange) { //IE<9
      var range = target.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
    }
  }
})(jQuery)
@@ -109,24 +109,39 @@
 th == 0) %7B%0D%0A
+      //HTML5%0D%0A
       if (ta
@@ -164,18 +164,8 @@
 art)
- %7B //HTML5
 %0D%0A  
@@ -174,17 +174,14 @@
     
-var pos =
+return
  tar
@@ -211,93 +211,47 @@
     
-  return pos %3E 0 ? pos : 0;%0D%0A      %7D%0D%0A      else if (target.createTextRange) %7B //IE%3C9
+//IE%3C9%0D%0A      if (document.selection) %7B
 %0D%0A  
@@ -290,16 +290,17 @@
 ar range
+1
  = docum
@@ -341,176 +341,94 @@
     
-if (
+var 
 range
+2
  =
-= null)%0D%0A            return '0';%0D%0A        var re = target.createTextRange();%0D%0A        var rc = re.duplicate();%0D%0A        re.moveToBookmark(range.getBookmark()
+ document.body.createTextRange();%0D%0A        range2.moveToElementText(target
 );%0D%0A
@@ -436,17 +436,21 @@
        r
-c
+ange2
 .setEndP
@@ -464,18 +464,20 @@
 ndTo
-Start', re
+End', range1
 );%0D%0A
@@ -492,17 +492,21 @@
 return r
-c
+ange2
 .text.le
@@ -530,23 +530,8 @@
     
- else return 0;
  //n
@@ -544,16 +544,33 @@
 ported%0D%0A
+      return 0;%0D%0A
     %7D%0D%0A 
@@ -575,24 +575,37 @@
 %0A    //set%0D%0A
+    //HTML5%0D%0A
     if (targ
@@ -625,24 +625,16 @@
 onRange)
- //HTML5
 %0D%0A      
@@ -666,24 +666,36 @@
 pos, pos);%0D%0A
+    //IE%3C9%0D%0A
     else if 
@@ -720,23 +720,16 @@
 Range) %7B
- //IE%3C9
 %0D%0A      
@@ -783,44 +783,17 @@
 nge.
-collapse(true);%0D%0A      range.moveEnd
+moveStart
 ('ch
@@ -825,34 +825,21 @@
 nge.
-moveStart('character', pos
+collapse(true
 );%0D%0A

simplify code