Last active
September 22, 2020 02:52
-
-
Save JenuelDev/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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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