w3schools get coordinates of cursor in jquery code example
Example: jquery get cursor position
Use selectionStart, it is compatible with all major browsers.
document.getElementById('foobar').addEventListener('keyup', e => {
console.log('Caret at: ', e.target.selectionStart)
})
<input id="foobar" />