Note that there are some explanatory texts on larger screens.

plurals
  1. POTest if cursor is on the first line in a textarea (with soft-newlines)
    primarykey
    data
    text
    <p>Given a textarea with content that flows like this</p> <pre> –––––––––––––––––––––––––– | This is some text, which | | wraps like this. | –––––––––––––––––––––––––– </pre> <p>How can one tell if the text-cursor is on the first line of the textarea?</p> <p>Obviously, checking for a newline character (<code>\n</code>) works if one wants to see if the cursor appears before the first line break, but testing for a 'soft' line break seems more challenging.</p> <p>Here is a sample <a href="http://jsfiddle.net/bmh_ca/h46jh/26/" rel="nofollow">jsFiddle to experiment with</a>.</p> <p>I have not yet come up with a strategy, but I suspect it may involve copying the text up until the cursor position into a cloned textarea (or div), and making the width as long as it needs to be so it doesn't wrap. If the cloned area has a width less than the original's width, then the cursor would seem to have to be on the first line. There may a simpler option, something more elegant, or (best of all) an existing and well-tested solution.</p> <p>Target browsers are Webkit (Chrome/Safari) &amp; Firefox. I.e. IE compatibility is not a concern at this time (if that makes any difference).</p> <p>Thanks for reading.</p> <h3>EDIT: Seeking line number of text caret, not mouse cursor.</h3> <p>falsarella gave an excellent answer, that highlighted an ambiguity in the question. What I am seeking is whether the text cursor (“caret” may be a better word) is on the first line. I have updated the question and <a href="http://jsfiddle.net/bmh_ca/h46jh/26/" rel="nofollow">the jsFiddle</a> to reflect.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload