views:

110

answers:

2

If I'm typing text in a input field and press ENTER the default behavior of all the browsers I know is to submit the form, however if I press ENTER inside a textarea a new line is added.

Is there any way to mimic this behavior (indent, not submit the form) whenever I press TAB inside a textarea? Bespin seems to do it, but in a canvas element.

+2  A: 

I haven't done it myself, but it seems to be possible to override the event handler and catch the key. See e.g. here.

Oh and for the JQuery crowd there even is a plugin. Boy, is this going to get me upvotes! :)

Pekka
+1 For the cause, thanks Pekka!
Alix Axel
+1  A: 

Of course there's a way. Do you use any js library? If not, the idea is just to add a keydown event handler on the textarea element, check in the handler if the keyCode of the event equals 9, and if so append a "\t" to the content of the textarea. Prototype snippet:

textarea.observe('keydown', function (e) {
  if(e.keyCode==9) {
    e.element().insert("\t");
    e.stop();
  }
}
Alsciende