Skip to content

Instantly share code, notes, and snippets.

@dotSlashLu
Created May 26, 2013 13:48
Show Gist options
  • Save dotSlashLu/5652854 to your computer and use it in GitHub Desktop.
Save dotSlashLu/5652854 to your computer and use it in GitHub Desktop.
Make textareas able to insert tabs
function tab(event, obj) {
var tabKeyCode = 9;
if (event.which) // mozilla
var keycode = event.which;
else // ie
var keycode = event.keyCode;
if (keycode == tabKeyCode) {
if (event.type == "keydown") {
if (obj.setSelectionRange) {
// mozilla
var s = obj.selectionStart;
var e = obj.selectionEnd;
obj.value = obj.value.substring(0, s) +
"\t" + obj.value.substr(e);
obj.setSelectionRange(s + 1, s + 1);
obj.focus();
} else if (obj.createTextRange) {
// ie
document.selection.createRange().text = "\t"
obj.onblur = function () {
this.focus();
this.onblur = null;
};
} else {
// unsupported browsers
}
}
if (event.returnValue) // ie ?
event.returnValue = false;
if (event.preventDefault) // dom
event.preventDefault();
return false; // should work in all browsers
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment