Question

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?

Was it helpful?

Solution 2

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").

OTHER TIPS

cm.getWrapperElement() → Element Returns the DOM node that represents the editor, and controls its size. Remove this from your tree to delete an editor instance.

See the documentation. http://codemirror.net/doc/manual.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top