JSFiddle - React, Tailwind, and code Playground

by Aaron von Schassen

HTML

<textarea id="the_id_value">the text is an example</textarea>  
  
<a href="#" onclick="alert( $('#the_id_value').getCursorPosition() );  
    return false;">getCursorPosition</a>

JavaScript

(function($, undefined) {  
    $.fn.getCursorPosition = function() {  
        var el = $(this).get(0);  
        var pos = 0;  
        if ('selectionStart' in el) {  
            pos = el.selectionStart;  
        } else if ('selection' in document) {  
            el.focus();  
            var Sel = document.selection.createRange();  
            var SelLength = document.selection.createRange().text.length;  
            Sel.moveStart('character', -el.value.length);  
            pos = Sel.text.length - SelLength;  
        }  
        return pos;  
    }  
})(jQuery);