Question

I am dynamically filling textarea using Ajax call's. In my code, I want to get the cursor's current line content using window.getSelection. I tried following code,

var range = window.getSelection().toString;
alert (typeof(range));

But, It returns function as alert Message. or Any other better way, to get the cursor's current line content from textarea that code need to support in all browser's.? Once I get the current line content I will find out line number and update new content on it.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top