tab button in textbox
An anoying thing is when pressing tab the textbox looses focus. It would be nice if it inserts a tab.
What I do is associate the keypress of a textbox with a function say txtkeypress().
Code:
// This code I got somewhere along time ago.
function insertAtCursor(myField, myValue) {
//IE support
var xs, ys;
// Save scrolling positions
xs = myField.scrollLeft;
ys = myField.scrollTop;
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//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 +1 ;
myField.selectionEnd = startPos + 1;
} else {
myField.value += myValue;
}
myField.scrollTop = ys;
myField.scrollLeft = xs;
}
function txtkeypress(txt, e) {
if(e.keyCode == 9){
insertAtCursor(txt, "\t");
return false;
}
txtChange(txt);
}
And tab inserts instead of loosing focus. Might not work for all browsers but atleast its a start.
Thanks
|