Given a text box with content that flows this way
βββββββββββββββββββββββββββ
| This is some text, which |
| wraps like this. |
βββββββββββββββββββββββββββ
How to determine if a text cursor is in the first line of a text field?
Obviously, checking for a newline character ( \n ) works if you want to see if the cursor appears before the first line break, but testing for soft line breaks seems more complicated.
Here is a jsFiddle example to experiment with .
I donβt have a strategy yet, but I suspect that this could be due to copying the text to the cursor position in the cloned text area (or div) and creating the width as long as it should be so, don't wrap it. If the cloned area has a width less than the original width, then the cursor seems to be on the first line. There may be a simpler option, something more elegant or (best of all) an existing and well-tested solution.
Target browsers are Webkit (Chrome / Safari) and Firefox. That is, compatibility with IE is currently not a problem (if that matters).
Thank you for reading.
EDIT: Searches for the line number of the text, not the mouse cursor.
falsarella gave an excellent answer that emphasized the ambiguity of the question. What I'm looking for is that the text cursor ("carriage" may be the best word) is on the first line. I updated the question and jsFiddle to reflect.
source share