In javascript, the event order is like this:
- keydown
- keypress
- change
- keyup
I get the current cursor position with this:
function getCursorPosition(event) {
var lines = event.target.value.slice(0, event.target.selectionStart)
return {
row: lines.length,
col: lines[lines.length - 1].length
}
}
If I put the function in onkeydown
or onkeypress
event, the return value
is the cursor position before text inserted
If I put the function in onchange
event, the function is not called when I hit
arrow
key or other key that triggers cursor movement.
If I put the function in onkeyup
event, the function is not called when I don't
release key (holding keydown).
Now, the only way I can think of is to put the function in both onchange
and
onkeyup
. Flag change
event in onchange
. Calls the function only if change
flag is not true on keyup
. And set change
flag on keyup to false.
Is there any better way?