How to determine if cursor position is on start of a defined row in textarea with JavaScript?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using the answer provided here:
Caret position in textarea, in characters from the start
you can get the offset of the cursor position in the text. After you have that, you can count the newline characters present in the text of the
<textarea>before the offset to figure out which line you are on. If the offset is just after a newline, then you’re at the start of the row. Using these two methods, you should be able to figure out if you are at the start of a specific line. For example:Note: IE might use carriage returns (\r) as well, so you may have to account for them.