I am writing a syntax highlighter. The highlighter should update the highlighting immediately while entering text and navigating with the arrow keys.
The problem I\'
Someone just mentioned on my request for an event for caret position changes that there is also a selectionchange event, which is fired at the document everytime the selection has changed.
This then allows to get the correct cursor position by calling window.getSelection()
.
Example:
function handleSelectionChange(evt) {
console.log(evt.type, window.getSelection().getRangeAt(0));
}
document.addEventListener("selectionchange", handleSelectionChange);
foo