سؤال

I am trying to use CodeMirror for syntax highlighting in a textarea. I am also using Bootstrap 2.3 for the textarea. I get the code in the textarea displayed by Bootstrap, but instead of the syntax highlighting in CodeMirror I get this error in the Chrome console:

Uncaught TypeError: Cannot set property 'display' of undefined.

This is the Javascript I use for the given form, the object is null.

var myCodeMirror = CodeMirror.fromTextArea($('#formId'), {
    mode: {name: "python",
           version: 2,
           singleLineStringErrors: false},
    lineNumbers: true,
    //indentUnit: 4,
    smartIndent: true,
    tabSize: 2,
    indentWithTabs: true,
    tabMode: "shift",
    autofocus: true,
    matchBrackets: true
  });
هل كانت مفيدة؟

المحلول

It would have been really helpful to get the full set of code in order to find the error. See: http://sscce.org/

The only thing I can do is to provide a piece of code where bootstrap and codemirror actually work together:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/2.3.2/js/bootstrap.min.js"></script>
  <link rel=stylesheet href="//codemirror.net/doc/docs.css">
  <link rel=stylesheet href="//codemirror.net/lib/codemirror.css">
  <link rel=stylesheet href="//codemirror.net/theme/night.css">
  <script src="//codemirror.net/lib/codemirror.js"></script>
  <script src="//codemirror.net/mode/python/python.js"></script>
</head>
<body>
<div class="container">
  <h2>Form control: textarea</h2>
  <p>The form below contains a textarea for python:</p>
  <form role="form">
    <div class="form-group">
      <label for="comment">Comment:</label>
      <textarea class="form-control" rows="5" id="comment">
# indent your Python code to put into an email
import glob
# glob supports Unix style pathname extensions
python_files = glob.glob('*.py')
for file_name in sorted(python_files):
    print '    ------' + file_name

    with open(file_name) as f:
        for line in f:
            print '    ' + line.rstrip()

    print
</textarea>
    </div>
  </form>
</div>  
    <script>
      var myCodeMirror = CodeMirror.fromTextArea(document.getElementById('comment'), {
        mode: {name: "python",
          version: 2,
          singleLineStringErrors: false},
        lineNumbers: true,
        //indentUnit: 4,
        smartIndent: true,
        tabSize: 2,
        indentWithTabs: true,
        tabMode: "shift",
        autofocus: true,
        matchBrackets: true        
      });
    </script>
</body>
</html>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top