Enable Tab Character Inside Textarea

Allows you to type Tab character to textarea (Multi-line text box)

  1. // ==UserScript==
  2. // @name Enable Tab Character Inside Textarea
  3. // @namespace userscript
  4. // @version 1.0
  5. // @description Allows you to type Tab character to textarea (Multi-line text box)
  6. // @author Anonymous, YFdyh000
  7. // @include http*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // https://jsfiddle.net/2wAzx/13/
  12. function enableTab(el) {
  13. el.onkeydown = function(e) {
  14. if (e.keyCode === 9) { // tab was pressed
  15.  
  16. // get caret position/selection
  17. var val = this.value,
  18. start = this.selectionStart,
  19. end = this.selectionEnd;
  20.  
  21. // set textarea value to: text before caret + tab + text after caret
  22. this.value = val.substring(0, start) + '\t' + val.substring(end);
  23.  
  24. // put caret at right position again
  25. this.selectionStart = this.selectionEnd = start + 1;
  26.  
  27. // prevent the focus lose
  28. return false;
  29.  
  30. }
  31. };
  32. }
  33. for (let el of document.querySelectorAll('textarea')) {
  34. enableTab(el);
  35. }