Jun
30
2014

Allow Tab in Textarea

If you’re a programmer, then you know exactly how irritating it can be to have a big blank textarea that you just happen to leave if you hit the tab key. Not anymore!

var textareas = document.getElementsByTagName('textarea');
var count = textareas.length;
for(i=0;i<count;i++){
    textareas[i].onkeydown = function(e){
        if(e.keyCode==9 || event.which==9){
            e.preventDefault();
            var s = this.selectionStart;
            this.value = this.value.substring(0,this.selectionStart) + "\t" + this.value.substring(this.selectionEnd);
            this.selectionEnd = s+1; 
        }
    }
}

Leave a comment