I am working on a project and I want to get the caret position in a textarea on the screen so I can display a container near to the cursor but everything I google says to get the element using getElementById
and then calling selectionStart
or selectionEnd
to get the cursor position.
But whenever I try that I get the error saying selectionStart does not exist on the type HTMLElement.
Below is the code I am trying:
const elem = document.getElementById("text1") as HTMLElement;
var curPos = elem.selectionStart;
I've also tried removing the as HTMLElement
in case that makes a difference but I get the same error.