JS event for capturing spacebar swipe to move cursor in input

Viewed 73

For mobile devices, when you swipe the spacebar in an input field, the cursor changes position. What JS event is used to capture that event?

1 Answers

Use selectionchange to detect changes of the caret/cursor:

document.onselectionchange = () => {
    var sel = document.getSelection()
    info.innerHTML =
       'anchorOffset: ' + sel.anchorOffset + '<br>' +
       'focusOffset: ' + sel.focusOffset + '<br>'
}
<p contenteditable>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua</p>

<div id="info">info</div>

https://jsfiddle.net/xz1upsk8

Related