How to display multiple CodeMirror correctly?

Viewed 42

This code re-adds information only to the first block:

window.onload = function() {
document.querySelectorAll(".codemirror-textarea").forEach(el => {
    const [output] = document.querySelectorAll(".codemirror-textarea");
    const editor = CodeMirror.fromTextArea(output, {lineNumbers: true, readOnly: true});
});}
2 Answers

You are looping over the elements, but applies CodeMirror to the first element each time, use el in your loop.

window.onload = function(e) {
    document.querySelectorAll(".codemirror-textarea").forEach(el => {
        const editor = CodeMirror.fromTextArea(el, {lineNumbers: true, readOnly: true});
    });
}

Another way:

window.onload = function() {
Array.from(document.querySelectorAll(".codemirror-textarea")).forEach(el => {
    CodeMirror.fromTextArea(el, {lineNumbers: true, readOnly: true});
});}
Related