[Solved] Failed to execute ‘getRangeAt‘ on ‘Selection‘: 0 is not a valid index.“

Failed to execute ‘getRangeAt‘ on ‘Selection‘: 0 is not a valid index.“

const sel = window.getSelection()
range = sel.getRangeAt(0)

The following error message appears when getting the object in the text area selected by the mouse

Solution:

const sel = window.getSelection()
for (let i = 0; i < sel.rangCount; i++) {
    range[i] = sel.getRangeAt(i)
}

Read More: