Skip to main content
improve clarity, remove thanks
Source Link
ggorlen
  • 59.3k
  • 8
  • 119
  • 173

Nice one, big thanks to Max.

I've wrapped the functionality in his answerbezmax's answer into jQuery if anyone wants to use it.

(function($) { $.fn.getCursorPosition = function() { var input = this.get(0); if (!input) return; // No (input) element found if ('selectionStart' in input) { // Standard-compliant browsers return input.selectionStart; } else if (document.selection) { // IE input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length); return sel.text.length - selLen; } } })(jQuery); 

Nice one, big thanks to Max.

I've wrapped the functionality in his answer into jQuery if anyone wants to use it.

(function($) { $.fn.getCursorPosition = function() { var input = this.get(0); if (!input) return; // No (input) element found if ('selectionStart' in input) { // Standard-compliant browsers return input.selectionStart; } else if (document.selection) { // IE input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length); return sel.text.length - selLen; } } })(jQuery); 

I've wrapped the functionality in bezmax's answer into jQuery if anyone wants to use it.

(function($) { $.fn.getCursorPosition = function() { var input = this.get(0); if (!input) return; // No (input) element found if ('selectionStart' in input) { // Standard-compliant browsers return input.selectionStart; } else if (document.selection) { // IE input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length); return sel.text.length - selLen; } } })(jQuery); 
Corrected plugin. Given priority to the standard method.
Source Link
Rob W
  • 350.5k
  • 87
  • 811
  • 683

Nice one, big thanks to Max.

I've wrapped the functionality in his answer into jQuery if anyone wants to use it..

new (function($) { $.fn.getCursorPosition = function() { var posinput = 0;this.get(0); varif (!input) =return; $// No (thisinput).get element found if (0'selectionStart' in input); {   // IEStandard-compliant Supportbrowsers return input.selectionStart;   } else if (document.selection) { // IE input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length);   pos =return sel.text.length - selLen; }   // Firefox support else if (input.selectionStart || input.selectionStart == '0') pos = input.selectionStart; return pos; } } )(jQuery); 

Nice one, big thanks to Max.

I've wrapped the functionality in his answer into jQuery if anyone wants to use it..

new function($) { $.fn.getCursorPosition = function() { var pos = 0; var input = $(this).get(0); // IE Support if (document.selection) { input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length);   pos = sel.text.length - selLen; }   // Firefox support else if (input.selectionStart || input.selectionStart == '0') pos = input.selectionStart; return pos; } } (jQuery); 

Nice one, big thanks to Max.

I've wrapped the functionality in his answer into jQuery if anyone wants to use it.

(function($) { $.fn.getCursorPosition = function() { var input = this.get(0); if (!input) return; // No (input) element found if ('selectionStart' in input) {   // Standard-compliant browsers return input.selectionStart;   } else if (document.selection) { // IE input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length); return sel.text.length - selLen; } } })(jQuery); 
Source Link
MarkB29
  • 4.9k
  • 4
  • 23
  • 15

Nice one, big thanks to Max.

I've wrapped the functionality in his answer into jQuery if anyone wants to use it..

new function($) { $.fn.getCursorPosition = function() { var pos = 0; var input = $(this).get(0); // IE Support if (document.selection) { input.focus(); var sel = document.selection.createRange(); var selLen = document.selection.createRange().text.length; sel.moveStart('character', -input.value.length); pos = sel.text.length - selLen; } // Firefox support else if (input.selectionStart || input.selectionStart == '0') pos = input.selectionStart; return pos; } } (jQuery);