views:

35

answers:

1

How can I find the value of the current line of a textarea?

I know I have to find the caret position, but then find everything before it up to the last \n and everything after it to the next \n.

How can I do this?

+1  A: 

A simple way would be to just loop:

var caretPos = 53, // however you get it
    start, end
;

for (start = caretPos; start >= 0 && myString[start] != "\n"; --start);
for (end = caretPos; end < myString.length && myString[end] != "\n"; ++end);

var line = myString.substring(start + 1, end - 1);
nickf
Works great thanks
Luke Burns