JSFiddle - React, Tailwind, and code Playground
by orthosie
HTML
<h1>
Get current cursor position in text input
</h1>
<input type="text" name="nameEdit" id="textt" value="">
JavaScript
/*
** Returns the caret (cursor) position of the specified text field.
** Return value range is 0-oField.value.length.
*/
function doGetCaretPosition (oField) {
// Initialize
var iCaretPos = 0;
// IE Support
if (document.selection) {
// Set focus on the element
oField.focus();
// To get cursor position, get empty selection range
var oSel = document.selection.createRange();
// Move selection start to 0 position
oSel.moveStart('character', -oField.value.length);
// The caret position is selection length
iCaretPos = oSel.text.length;
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0')
iCaretPos = oField.selectionStart;
// Return results
return iCaretPos;
}
$(document).ready(function() {
$('#textt').keyup(function(event) {
t = doGetCaretPosition(event.currentTarget);
console.log(t);
});
} );