Ad

How Do I Detect Whether A Position Is At The Beginning Of A Line In A Textarea?

- 1 answer

I have a textarea like this:

<textarea>
this is test1
and this is test2
plus it is test3
</textarea>

As you see, this positions are in the beginning of line: 0 (t), 14 (a), 32 (p).


Now I need to determine whether a position (I mean is the number of position) is at the beginning of a line?

For example:

10 : false
5  : false
14 : true
40 : false

How can I do that?

Ad

Answer

if you want to know that the n-th position is at the beginning, then:

n == 0 || $("#textbox").val()[n-1] == '\n'
Ad
source: stackoverflow.com
Ad