Using the syntax text_element.selectionStart
we can get the starting position of the selection of a text in terms of the index of the first character of the selected text in the text_element.value
and in case we want to get the same of the last character in the selection we have to use text_element.selectionEnd
.
Use it as follows:
<input type=text id=t1 value=abcd>
<button onclick="alert(document.getElementById('t1').selectionStart)">check position</button>
I'm giving you the fiddle_demo