tags:

views:

128

answers:

3

Hi,

I am trying to create a webform in HTML, and if needed javascript. In this webform one should be able to enter source code, so to do that comfortably I would like one to be able to enter tabs. Is there a way to achieve this?

Thanks

+2  A: 

You might be able to capture the onKeyDown event. If the keycode is equal to the tab key, then replace the tab with 3 spaces or something like that.

UPDATE: I tested this in firefox 3. Allows you to type a tab without loosing focus. Just be careful b/c this code will just append a tab character to the end of the text box. Thus, if the user types a tab in the middle of text, tab will still appear at the end.

<html>
<head>
<script>
    function kH(e) 
    {
        //capture key events
        var pK = document.all? window.event.keyCode:e.which;

        //if target is textbox and key is tab
        if(e.target.type=='text' && pK==0)
        {
            //append tab to end of target value
            e.target.value = e.target.value + "\t";

            //Cancel key event
            return false;
        }

    }

   document.onkeypress = kH;
   if (document.layers) document.captureEvents(Event.KEYPRESS);

</script>
</head>
<form>
    <input type='text' id='txtTest' name='txtTest'></input>
</form>
</html>
J.Hendrix
that is what I feared. Thanks for the answer
data
A: 

There isn't a good way...that's why stackoverflow makes you do 4 spaces and uses a special library to interpret 4-space indented stuff as code. I suppose if you really wanted to use tabs you could do an onBlur event handler which just gave focus back to the window, and an onKeyDown event handler that inserted 4 spaces whenever the TAB key was pressed.

Brian Schroth
That is what I figured. Some of my parsers use similar coding style.
data
A: 

You could go for an "indent" button on the toolbar. When it is pressed, it either inserts a tab if nothing is selected, or indents the selection.

DisgruntledGoat