Search code examples
javascripteditorinstancecodemirror

CodeMirror delete a editor instance


I am using CodeMirror to do some syntax highlighting in my web. I've created two radio buttons in my html. And my JavaScript code is as follows:

if(flag == "C")
    Editor = CodeMirror.fromTextArea(textArea, {
    lineNumbers: true,
    matchBrackets: true,
    mode: "text/x-csrc"
});
else if(flag == "Cpp")
    Editor.off(0);
    Editor = CodeMirror.fromTextArea(textArea, {
    lineNumbers: true,
    matchBrackets: true,
    mode: "text/x-c++src"
});

It does complete the function of syntax highlighting, but the problem is when I click the radio button multiple times, it just create as many as I click, and this is annoying. So I am wondering is there any way to delete the previous editor instance before I create a new one?


Solution

  • Editors created with fromTextArea have a toTextArea method to remove them. Alternatively, simply create the editor once and use Editor.setOption("mode", "text/x-csrc").