Skip to content

Instantly share code, notes, and snippets.

@albburtsev
Last active December 12, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albburtsev/4713846 to your computer and use it in GitHub Desktop.
Save albburtsev/4713846 to your computer and use it in GitHub Desktop.
jQuery textarea tab insert
/*
* Textarea tab insert
* @dependence jQuery>=1.4.2
* @dependence https://github.com/jeresig/jquery.hotkeys
* @author Alexander Burtsev
*/
$('textarea', _form).bind("keydown", "tab", function(e) {
if ( document.selection ) { // IE
var iesel = document.selection.createRange().duplicate();
iesel.text = "\t";
} else {
var start = this.selectionStart,
left = this.value.substring(0, start),
right = this.value.substring(this.selectionEnd),
scroll = this.scrollTop;
this.value = left + "\t" + right;
this.selectionStart = this.selectionEnd = start + 1;
this.scrollTop = scroll;
this.focus();
}
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment