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
2009-02-15 11:37:09
Meta-Filter is working again so I edited your answer.
Unkwntech
2009-02-16 02:19:53
+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 :)
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
2009-02-15 12:52:19
@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
2009-02-15 12:52:31
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
2009-02-16 05:51:42
+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
2009-02-15 11:47:46
+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
2009-02-15 11:49:27