Skip to content

Instantly share code, notes, and snippets.

@unknownuser88
Created January 6, 2014 15:51
Show Gist options
  • Save unknownuser88/8284797 to your computer and use it in GitHub Desktop.
Save unknownuser88/8284797 to your computer and use it in GitHub Desktop.
6 JQUERY CURSOR FUNCTIONS
(function($, len, createRange, duplicate){
"use strict";
$.fn.caret = function(options,opt2) {
if ( typeof this[0] === 'undefined' || this.is(':hidden') || this.css('visibility') === 'hidden' ) { return this; }
var n, s, start, e, end, selRange, range, stored_range, te, val,
selection = document.selection, t = this[0], sTop = t.scrollTop,
ss = typeof t.selectionStart !== 'undefined';
if (typeof options === 'number' && typeof opt2 === 'number') {
start = options;
end = opt2;
}
if (typeof start !== 'undefined') {
if (ss){
t.selectionStart=start;
t.selectionEnd=end;
} else {
selRange = t.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end-start);
selRange.select();
}
// must be visible or IE8 crashes; IE9 in compatibility mode works fine - issue #56
if (this.is(':visible') || this.css('visibility') !== 'hidden') { this.focus(); }
t.scrollTop = sTop;
return this;
} else {
if (ss) {
s = t.selectionStart;
e = t.selectionEnd;
} else {
if (t.tagName === 'TEXTAREA') {
val = this.val();
range = selection[createRange]();
stored_range = range[duplicate]();
stored_range.moveToElementText(t);
stored_range.setEndPoint('EndToEnd', range);
// thanks to the awesome comments in the rangy plugin
s = stored_range.text.replace(/\r/g, '\n')[len];
e = s + range.text.replace(/\r/g, '\n')[len];
} else {
val = this.val().replace(/\r/g, '\n');
range = selection[createRange]()[duplicate]();
range.moveEnd('character', val[len]);
s = (range.text === '' ? val[len] : val.lastIndexOf(range.text));
range = selection[createRange]()[duplicate]();
range.moveStart('character', -val[len]);
e = range.text[len];
}
}
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[len]);
}};
}
};
})(jQuery, 'length', 'createRange', 'duplicate');
//Example jQuery get cursor position function call
$("input[name='username']").getCursorPosition();
jQuery.fn.getCursorPosition = function(){
if(this.lengh == 0) return -1;
return $(this).getSelectionStart();
}
//Example jQuery set cursor position function call
$("input[name='username']").setCursorPosition(5);
jQuery.fn.setCursorPosition = function(position){
if(this.lengh == 0) return this;
return $(this).setSelection(position, position);
}
//Example jQuery get text selection function call
$("input[name='username']").getSelection();
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);
}
//Example jQuery get text selection start function call
$("input[name='username']").getSelectionStart();
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;
}
//Example jQuery get text selection end function call
$("input[name='username']").getSelectionEnd();
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;
}
//Example jQuery set text selection function call
$("input[name='username']").setSelection(4, 20);
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment