views:

72

answers:

1

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

+1  A: 

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.

TNi
@TNi: The Chrome solution is quite helpful. I'm trying to achieve a sensible interface for extraordinarily long documents (i.e. >5,000 printed pages), so a single textarea is impossible. Thus I'm using in-place editing with textareas split by paragraphs. The anticipated user experience when pressing the down-key at the end of a paragraph is to move to the next editable paragraph.
Brian M. Hunt
Of course that's fine, as long as you've considered carefully the user experience.
TNi