Javascript/jQuery move to next field on keypress

When a user presses the down arrow key in a textarea, and it's the bottom of the textarea (i.e. the keyup has no effect), I'd like to an event handler to capture this.

$("<textarea />").keyup(function (e) {
    switch (e.keyCode) {
        case 40: // down
          // if last line or alternatively
          // if keypress does not change index
          // do something.
          break;
    }
});

Is it possible to achieve this functionality?

As well, I'd be interested in knowing how to capture the opposite - keycode 38 ("up") when at the top of the textarea.

EDIT: Incidentally, I'm using the jQuery FieldSelection plugin, if that's of any assistance.

Thank you for reading; obliged for any input.

Brian

Answers


Documentation on the FieldSelection plugin seems scarce, but look at the code in the update function on this page. You should be able to gather how to detect if the caret moves; this isn't exactly what you want, but it's close. What is the reason for doing this?

EDIT: I saw your comment to Jonathan's answer. In Chrome, at least, the last down arrow will move the caret to the end of the textarea, which you should be able to detect as per above. However, keep in mind your users will need to be aware of this "feature." You might consider making it more transparent just by having a link allowing users to add textareas.


Need Your Help

No Data Posted in a form

javascript jquery ajax forms post

I use a form which sends data with a POST method to a php file via AJAX. Everything is ok and I can see the results in the html table in its id div name. But now this very same table contains input