tabkey_in_textarea.js
677 Bytes
$(document).ready(function() {
$("textarea").keydown(function(e) {
if(e.keyCode === 9) { // tab was pressed
// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;
var value = $(this).val();
// set textarea value to: text before caret + tab + text after caret
$(this).val(value.substring(0, start) + " " + value.substring(end));
// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 4;
e.preventDefault(); // prevent the focus lose
}
});
});