Firstly, textareas have a different selection API from regular content: use selectionStart
and selectionEnd
properties of the textarea rather than window.getSelection()
.
Secondly, getting the current line requires some creative coding if you're accounting for the browser's auotmatic wrapping of content. I've seen a few questions about this in Stack Overflow. Here's one example:
finding "line-breaks" in textarea that is word-wrapping ARABIC text