Use tab to indent in textarea Use tab to indent in textarea javascript javascript

Use tab to indent in textarea


Borrowing heavily from other answers for similar questions (posted below)...

document.getElementById('textbox').addEventListener('keydown', function(e) {  if (e.key == 'Tab') {    e.preventDefault();    var start = this.selectionStart;    var end = this.selectionEnd;    // set textarea value to: text before caret + tab + text after caret    this.value = this.value.substring(0, start) +      "\t" + this.value.substring(end);    // put caret at right position again    this.selectionStart =      this.selectionEnd = start + 1;  }});
<input type="text" name="test1" /><textarea id="textbox" name="test2"></textarea><input type="text" name="test3" />