Here's one way to allow tabs in an element that supports it - like a textarea.
works in ie,ff,safari,opera
tested on windows, ubuntu
Code:
(function(){
var _checkfortab = (document.selection)?
function(e){
e.onkeydown=
function(){
if(event.keyCode==9){
this.focus();
sel = document.selection.createRange();
sel.text = String.fromCharCode(9);
return false;
}
}
}:
function(e){
e.onkeydown=
function(e){
if(e.keyCode==9){
var startPos =this.selectionStart;
var endPos = this.selectionEnd;
this.value = this.value.substring(0, startPos)+ String.fromCharCode(9)+ this.value.substring(endPos, this.value.length);
return false;
}
}
}
window.allowTabs=function(el){_checkfortab(el)};
})();
You use it like this:
Code:
allowTabs(textareaElement);
A specific example:
Code:
allowTabs(getElementsByTagName("textarea")[0]);
allowTabs is a global function due to the last line in the code block.
Code:
window.allowTabs=function(el){_checkfortab(el)};
You can change this from a global function to a library member by changing that line of code to return an object and add a variable to recieve the object.
like this (assuming myLib is an object):
Code:
var myLib.editbox = (function(){
...
return {allowTabs:function(el){_checkfortab(el)}};
})()
now you would use it like this:
Code:
myLib.editbox.allowTabs(getElementsByTagName("textarea")[0]);
The result is a textarea that cpatures tabs and prints them into the textarea when you hit the tab key.
Don't forget to make sure the element exists before you try to use allowTabs on it
Thanks,