views:

249

answers:

6

How can I make a text box that allows users to enter tabs, and does not send the user to the next element when the tab button is pressed?

+2  A: 

You can use JavaScript to catch the tab keypress event and replace it with spaces (I'm not sure about inserting tabs into a textarea).

E: This page looks good.

Ross
Meta-Filter is working again so I edited your answer.
Unkwntech
+1  A: 

onkeypress, onkeyup or onkeydown check the key that was pressed and if it is a tab then append \t to the textbox and return false so that focus remains on the textbox you will most likely have to use textranges so that tabs can be inserted anywhere not at the end of the text that's the basic idea for the rest google is your friend :)

+3  A: 

There are already some plug-ins for jQuery that do this. One for example is Tabby.

Gumbo
Who said anything about jQuery?
Unkwntech
I said something about jQuery. Because: Why not jQuery? Sure, it’s a little exaggerated. But perhaps he want to use it also for other things. And when he uses Google API <http://ajax.googleapis.com/ajax/libs/jquery/…> it may be already in cache.
Gumbo
@Unkwntech -- no need to reinvent the wheel. Pointing out an existing solution to a problem is perfectly legitimate. If you had said "I use MooTools (or Prototype)" then pointing out a jQuery solution might not be appropriate.
tvanfosson
I don't think it's nesicary to import a 54KB library plus a plug-in to do something that can be done in like 20KB or less.
Unkwntech
+7  A: 

You only need to check for tabs onkeydown via event.keyCode === 9. Inserting the character into the textarea is non-trivial - use a library or google for 'insertatcaret'.

Christoph
+2  A: 
<textarea onkeydown="return catchTab(this, event);">

JS code:


function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
     input.focus();
     input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
     var range = input.createTextRange();
     range.collapse(true);
     range.moveEnd('character', selectionEnd);
     range.moveStart('character', selectionStart);
     range.select();
    }
}

function replaceSelection (input, replaceString) {
    if (input.setSelectionRange) {
     var selectionStart = input.selectionStart;
     var selectionEnd = input.selectionEnd;
     input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
     if (selectionStart != selectionEnd){
      setSelectionRange(input, selectionStart, selectionStart +  replaceString.length);
     } else{
      setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
     }
    } else if (document.selection) {
     var range = document.selection.createRange();
     if (range.parentElement() == input) {
      var isCollapsed = range.text == '';
      range.text = replaceString;
       if (!isCollapsed) {
       range.moveStart('character', -replaceString.length);
       range.select();
      }
     }
    }
}

function catchTab(item,e){
    if(navigator.userAgent.match("Gecko")){
     c=e.which;
    } else{
     c=e.keyCode;
    }
    if(c==9){
     replaceSelection(item, "\t");
     setTimeout(function() { item.focus() } , 0);
     return false;
    }
} 

thomask
A: 

Do NOT try to capture the onkeypress event for the 'TAB' key in IE (it doesn't work) (bug 249)

Try onkeydown or onkeyup instead.

scunliffe