Is it possible to retrieve the index into a text area that the cursor is at using the keydown event?
Asked
Active
Viewed 85 times
2 Answers
2
Yes, it is.
textarea.addEventListener('keydown', function (){
console.log(textarea.selectionEnd); //The position of the cursor. If something is selected, then the position of the end of the selection.
}, false);
This function won't work for IE. In IE you'd have to use document.createTextRange()
.

Some Guy
- 15,854
- 10
- 58
- 67
-
I know it works in Firefox, Safari. Does it work in IE? – Akhil Dec 22 '11 at 17:54
-
1@Akhil As I've said in the answer, no, it won't. – Some Guy Dec 22 '11 at 17:57
0
Please refer the below link for the implementation. You can call the getCaret
function on your keydown
event to get the index of the caret: