Revision: 40706
                            
                                                            
                                    
                                        
Updated Code
                                    
                                    
                                                    
                        at February 8, 2011 03:15 by crutzdesigns
                            
                            Updated Code
/** * Cursor Functions * * Used for setting and getting text cursor position within an input * and textarea field. Also used to get and set selection range. * * @author Branden Cash * @email [email protected] */ (function( $ ){ jQuery.fn.getCursorPosition = function(){ if(this.lengh == 0) return -1; return $(this).getSelectionStart(); } jQuery.fn.setCursorPosition = function(position){ if(this.lengh == 0) return this; return $(this).setSelection(position, position); } jQuery.fn.getSelection = function(){ if(this.lengh == 0) return -1; var s = $(this).getSelectionStart(); var e = $(this).getSelectionEnd(); return this[0].value.substring(s,e); } jQuery.fn.getSelectionStart = function(){ if(this.lengh == 0) return -1; input = this[0]; var pos = input.value.length; if (input.createTextRange) { var r = document.selection.createRange().duplicate(); r.moveEnd('character', input.value.length); if (r.text == '') pos = input.value.length; pos = input.value.lastIndexOf(r.text); } else if(typeof(input.selectionStart)!="undefined") pos = input.selectionStart; return pos; } jQuery.fn.getSelectionEnd = function(){ if(this.lengh == 0) return -1; input = this[0]; var pos = input.value.length; if (input.createTextRange) { var r = document.selection.createRange().duplicate(); r.moveStart('character', -input.value.length); if (r.text == '') pos = input.value.length; pos = input.value.lastIndexOf(r.text); } else if(typeof(input.selectionEnd)!="undefined") pos = input.selectionEnd; return pos; } jQuery.fn.setSelection = function(selectionStart, selectionEnd) { if(this.lengh == 0) return this; input = this[0]; if (input.createTextRange) { var range = input.createTextRange(); range.collapse(true); range.moveEnd('character', selectionEnd); range.moveStart('character', selectionStart); range.select(); } else if (input.setSelectionRange) { input.focus(); input.setSelectionRange(selectionStart, selectionEnd); } return this; } })( jQuery );
                            Revision: 40705
                            
                                                            
                                    
                                        
Updated Code
                                    
                                    
                                                    
                        at February 8, 2011 03:05 by crutzdesigns
                            
                            Updated Code
(function( $ ){
  jQuery.fn.getCursorPosition = function(){
    if(this.lengh == 0) return -1;
    return $(this).getSelectionStart();
  }
  
  jQuery.fn.setCursorPosition = function(position){
    if(this.lengh == 0) return this;
    return $(this).setSelection(position, position);
  }
  
  jQuery.fn.getSelection = function(){
    if(this.lengh == 0) return -1;
    var s = $(this).getSelectionStart();
    var e = $(this).getSelectionEnd();
    return this[0].value.substring(s,e);
  }
  
  jQuery.fn.getSelectionStart = function(){
    if(this.lengh == 0) return -1;
    input = this[0];
    
    var pos = input.value.length;
    
    if (input.createTextRange) {
      var r = document.selection.createRange().duplicate();
      r.moveEnd('character', input.value.length);
      if (r.text == '') 
        pos = input.value.length;
      pos = input.value.lastIndexOf(r.text);
    } else if(typeof(input.selectionStart)!="undefined")
      pos = input.selectionStart;
    
    return pos;
  }
  
  jQuery.fn.getSelectionEnd = function(){
    if(this.lengh == 0) return -1;
    input = this[0];
    
    var pos = input.value.length;
    
    if (input.createTextRange) {
      var r = document.selection.createRange().duplicate();
      r.moveStart('character', -input.value.length);
      if (r.text == '') 
        pos = input.value.length;
      pos = input.value.lastIndexOf(r.text);
    } else if(typeof(input.selectionEnd)!="undefined")
      pos = input.selectionEnd;
    
    return pos;
  }
  
  jQuery.fn.setSelection = function(selectionStart, selectionEnd) {
    if(this.lengh == 0) return this;
    input = this[0];
    
    if (input.createTextRange) {
      var range = input.createTextRange();
      range.collapse(true);
      range.moveEnd('character', selectionEnd);
      range.moveStart('character', selectionStart);
      range.select();
    } else if (input.setSelectionRange) {
      input.focus();
      input.setSelectionRange(selectionStart, selectionEnd);
    }
    
    return this;
  }
})( jQuery );
                                
                            Revision: 40704
                            
                                                            
                                    
                                        
Initial Code
                                    
                                    
                                                            
                                    
                                        
Initial URL
                                    
                                    
                                
                                                            
                                    
                                        
Initial Description
                                    
                                    
                                                            
                                    
                                        
Initial Title
                                    
                                    
                                                            
                                    
                                        
Initial Tags
                                    
                                    
                                                            
                                    
                                        
Initial Language
                                    
                                    
                                                    
                        at February 5, 2011 09:48 by crutzdesigns
                            
                            Initial Code
(function( $ ){
  jQuery.fn.getCursorPosition = function(){
    if(this.lengh == 0) return -1;
    return $(this).getSelectionStart();
  }
  
  jQuery.fn.setCursorPosition = function(position){
    if(this.lengh == 0) return this;
    return $(this).setSelection(position, position);
  }
  
  jQuery.fn.getSelection = function(){
    if(this.lengh == 0) return -1;
    var s = $(this).getSelectionStart();
    var e = $(this).getSelectionEnd();
    return this[0].value.substring(s,e);
  }
  
  jQuery.fn.getSelectionStart = function(){
    if(this.lengh == 0) return -1;
    input = this[0];
    
    var pos = input.value.length;
    
    if (input.createTextRange) {
      var r = document.selection.createRange().duplicate();
      r.moveEnd('character', input.value.length);
      if (r.text == '') 
        pos = input.value.length;
      pos = input.value.lastIndexOf(r.text);
    } else if(typeof(input.selectionStart)!="undefined")
      pos = input.selectionStart;
    
    return pos;
  }
  
  jQuery.fn.getSelectionEnd = function(){
    if(this.lengh == 0) return -1;
    input = this[0];
    
    var pos = input.value.length;
    
    if (input.createTextRange) {
      var r = document.selection.createRange().duplicate();
      r.moveStart('character', -input.value.length);
      if (r.text == '') 
        pos = input.value.length;
      pos = input.value.lastIndexOf(r.text);
    } else if(typeof(input.selectionEnd)!="undefined")
      pos = input.selectionEnd;
    
    return pos;
  }
  
  jQuery.fn.setSelection = function(selectionStart, selectionEnd) {
    if(this.lengh == 0) return this;
    input = this[0];
    
    if (input.createTextRange) {
      var range = input.createTextRange();
      range.collapse(true);
      range.moveEnd('character', selectionEnd);
      range.moveStart('character', selectionStart);
      range.select();
    } else if (input.setSelectionRange) {
      input.focus();
      input.setSelectionRange(selectionStart, selectionEnd);
      console.log(input);
    }
    
    return this;
  }
})( jQuery );
                                Initial URL
Initial Description
Used on `<input>`'s and `<textarea>`'s
Several jQuery functions for getting the current cursor position and setting the current cursor position. Also allows for selecting text in a certain range.
Usage:
<pre>
  $("input[name='username']").getCursorPosition();
  $("input[name='username']").setCursorPosition(5);
  $("input[name='username']").getSelection();
  $("input[name='username']").getSelectionStart();
  $("input[name='username']").getSelectionEnd();
  $("input[name='username']").setSelection(4, 20);
</pre>
                                Initial Title
jQuery Cursor Functions
Initial Tags
jquery
Initial Language
jQuery