Get index of selected text inside text input
If you have an HTML element input
, how can you determine the index of the start and end positions of the selected text inside input
? I tried to use window.getSelection
it but it doesn't work correctly. I need to figure this out in an event keydown
.
+3
Justin meltzer
source
to share
1 answer
You can use properties selectionStart
and selectionEnd
to get the indices of the corresponding values.
var start = document.getElementById("myArea").selectionStart;
var end = document.getElementById("myArea").selectionEnd;
console.log(start);
console.log(end);
+3
John Koerner
source
to share