Skip to content

Instantly share code, notes, and snippets.

@BroJenuel
Last active September 22, 2020 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BroJenuel/e44de5b822ed5a58d51b0d40768c82e7 to your computer and use it in GitHub Desktop.
Save BroJenuel/e44de5b822ed5a58d51b0d40768c82e7 to your computer and use it in GitHub Desktop.
This is a function that will be called when pressing tab when writing inside a text area element in html. This will create 4 spaces just like a tab.
$('#textarea').keydown(function (e) {
var keyCode = e.keyCode || e.which;
if (keyCode === $.ui.keyCode.TAB) {
e.preventDefault();
const TAB_SIZE = 4;
// The one-liner that does the magic
document.execCommand('insertText', false, ' '.repeat(TAB_SIZE));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment