showing results for - "tab adds tab textarea javascript"
Marion
09 May 2018
1document.querySelector('#textarea').addEventListener('keydown', e => {
2	if ( e.key === 'Tab' && !e.shiftKey ) {
3		// execCommand operations are "Cmd|Ctrl+Z"-able
4      	// note: execCommand is deprecated and may not work in the future
5		document.execCommand('insertText', false, "\t");
6		e.preventDefault();
7		return false;
8	}
9});
Lisa
25 Nov 2016
1$(document).delegate('#textbox', 'keydown', function(e) {
2  var keyCode = e.keyCode || e.which;
3
4  if (keyCode == 9) {
5    e.preventDefault();
6    var start = this.selectionStart;
7    var end = this.selectionEnd;
8
9    // set textarea value to: text before caret + tab + text after caret
10    $(this).val($(this).val().substring(0, start)
11                + "\t"
12                + $(this).val().substring(end));
13
14    // put caret at right position again
15    this.selectionStart =
16    this.selectionEnd = start + 1;
17  }
18});