views:

447

answers:

2

Does anyone know a cross-browser, reliable solution for catching presses of the tab-key in a textarea field, and replacing (in the correct position) 4 spaces? The textarea is being used to input an essay, and needs this feature.

Note: I tried using FCKEditor, among others, which did not catch tabs and had a bunch of features I didn't need. I want a simple solution just for catching tabs.

A: 

Is there some reason you can't just replace the tabs after the editing is done? I've played around quite a bit with replacing text while typing in a textarea, and found it to be... impractical at best.

Dave
The point is that the user should be able to insert "tabs" as he uses the page, but by default the tab key cycles focus to the next element.
Then you're getting into cross-browser implementations of onkeydown, which is something I've never been brave enough to try. Good luck!
Dave
+2  A: 

I didn't test extensively, but this seems to work:

(I found the "insertAtCursor" function at http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript#comment-3817)

<textarea id="text-area" rows="20" cols="100"></textarea>

<script>
document.getElementById("text-area").onkeydown = function(e) {
  if (!e && event.keyCode == 9)
  {
    event.returnValue = false;
    insertAtCursor(document.getElementById("text-area"), "    ");
  }
  else if (e.keyCode == 9)
  {
    e.preventDefault();
    insertAtCursor(document.getElementById("text-area"), "    ");
  }
};

//http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript#comment-3817
function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    var temp;
    myField.focus();
    sel = document.selection.createRange();
    temp = sel.text.length;
    sel.text = myValue;
    if (myValue.length == 0) {
      sel.moveStart('character', myValue.length);
      sel.moveEnd('character', myValue.length);
    } else {
      sel.moveStart('character', -myValue.length + temp);
    }
    sel.select();
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    myField.selectionStart = startPos + myValue.length;
    myField.selectionEnd = startPos + myValue.length;
  } else {
    myField.value += myValue;
  }
}
</script>

EDIT: Modified the script so it doesn't use jQuery.

Emmett