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"
});
}
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.
Codemirror will show error when using elements get from
getElementsByClassName
, however, if I change the code togetElementById
, 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" }); }