I need to determine if a user is on the first line, or last line, of text in a textarea.
The reason for this is that the client has requested that when a user presses the up arrow while on the top line of text in a textarea they are taken to the previous form field, or if on the last line of text and pressing the down arrow the user is taken to the following form field.
I have found a way to determine which row of text the cursor is on, but it only works if the rows of the textarea are defined. Most of the the textarea’s on the page are using the jQuery TextAreaExpander plugin, so their rows are not set.
The following combination of HTML and JavaScript works for me in Chrome:
HTML:
JavaScript (using jQuery):
Live Demo
In this example I am relying on an HTML5 property: selectionStart. I do not know if this will work in older browsers.