JSFiddle - React, Tailwind, and code Playground
by ozzon91
HTML
<textarea></textarea>
<textarea></textarea>
<textarea></textarea>
JavaScript
$("textarea").keydown(function(e) {
if(e.keyCode === 9) { // tab was pressed
// get caret position/selection
var start = this.selectionStart;
end = this.selectionEnd;
var $this = $(this);
// 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.selectionStart = this.selectionEnd = start + 1;
// prevent the focus lose
return false;
}
});