views:

552

answers:

2

Web browsers are good as thin clients for web applications.

But if the user has to enter some code (where tabs and formatting are important) in an edit box, inside a web browser, he navigates through the webpage controls every time he hits the "tab" key, instead of printing the "tab" character.

Are there any free web controls or is there any code to get the opposite behavior?
Google Docs does this perfectly.

Thanks.

+3  A: 

Yahoo UI library has a rich text editor that handles tab characters properly.

EDIT: I suspect (because I haven't looked) that both the YUI editor and Google's editor accomplish this by adding a onKeyPress handler to the text container. When they detect a tab character they append a tab to the container and return false to cancel the normal tab action.

tvanfosson
+2  A: 

You can filter the keyDown event and catch the tab key:

<html>
<body>
<script type="text/javascript">
function keyFilter(e, field) {
  var key = window.event ? e.keyCode : e.which;
  if (key == 9) {
     field.value += "\t";
     return false;
  }
  return true;
}
</script>

<form>
<textarea onkeydown="return keyFilter(event, this);"></textarea>
</form>
</html>

EDIT: Note that it's of course more complicated than this. One needs to keep track on where in the text the tab key is pressed and insert the character accordingly.

PEZ
On my Macintosh in Firefox, you need to check e.keyCode for a tab character as e.which is not set.
tvanfosson
Anyone knows how to do it (get the keycode) properly?
PEZ
Ummm... On my Mac in Firefox the above code works. And it works in Safari too.
PEZ
FF3 or FF2 -- I'm using FF2.
tvanfosson
I'm using FF3. I thought it was only IE that was special about the keycode.
PEZ