Created
August 21, 2014 22:48
-
-
Save gurel/dcbe4a799ae7008ad6a4 to your computer and use it in GitHub Desktop.
Textarea tab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).delegate('#textbox', 'keydown', function(e) { | |
var keyCode = e.keyCode || e.which; | |
if (keyCode == 9) { | |
e.preventDefault(); | |
var start = $(this).get(0).selectionStart; | |
var end = $(this).get(0).selectionEnd; | |
// set textarea value to: text before caret + tab + text after caret | |
$(this).val($(this).val().substring(0, start) | |
+ "\t" | |
+ $(this).val().substring(end)); | |
// put caret at right position again | |
$(this).get(0).selectionStart = | |
$(this).get(0).selectionEnd = start + 1; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment