Format code while typing using JavaScript

If you want to format the code while typing, you can use CodeMirror:

html
<!DOCTYPE html>
<html>
<head>
  <meta charset = 'utf-8'>
  <title>Edit code while typing</title>
  <script src = 'http://codemirror.net/lib/codemirror.js'></script>
  <script src = 'https://codemirror.net/mode/javascript/javascript.js'></script>
  <link rel = 'stylesheet' href = 'http://codemirror.net/lib/codemirror.css'>
  <link rel = 'stylesheet' href = 'http://codemirror.net/theme/monokai.css'>
</head>
<body>
    <textarea id = 'editor' autofocus = 'true'></textarea>
</body>
<script>
    var editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
        theme: 'monokai',
        mode: 'javascript',
        lineNumbers: true
    });
    editor.save();
</script>
</html>

Here you have a skeleton using the Monokai theme.

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.