codemirror / codemirror5

In-browser code editor (version 5, legacy)
http://codemirror.net/5/
MIT License
26.84k stars 4.97k forks source link

Codemirror will show error when using elements get from getElementsByClassName #7067

Closed gaoe6661 closed 1 year ago

gaoe6661 commented 1 year ago

Codemirror will show error when using elements get from getElementsByClassName, however, if I change the code to getElementById, it will work.

Error: codemirror_6.65.7_codemirror.min.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'body') at N (codemirror_6.65.7_codemirror.min.js:1:1800) at p.fromTextArea (codemirror_6.65.7_codemirror.min.js:1:168905) at loadHighlight (highlight.js:30:20) at load (highlight.js:23:5) at window.onload (model.html?u=/lessons/ch/1.html:12:17) N @ codemirror_6.65.7_codemirror.min.js:1 p.fromTextArea @ codemirror_6.65.7_codemirror.min.js:1 loadCodeHighlight @ highlight.js:30 load @ highlight.js:23 window.onload @ model.html?u=/lessons/ch/1.html:12 load(async) (anonymous) @ model.html?u=/lessons/ch/1.html:11

Example code: var elements = document.getElementsByClassName("codeTextarea"); for(var a in elements){ CodeMirror.fromTextArea(elements[a], { lineNumbers: true, mode: "text/x-csharp" }); }

marijnh commented 1 year ago

This is definitely going to be related to the actual value you're passing in, not whether it is coming from getElementById or getElementsByClassName. It is not a CodeMirror problem.