Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 434
user2992389 Avatar asked Nov 14 '13 14:11

user2992389


2 Answers

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

like image 92
Bren1818 Avatar answered Oct 01 '22 02:10

Bren1818


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

like image 41
Marijn Avatar answered Oct 01 '22 01:10

Marijn