Search code examples
javascriptcodemirror

How to kill a CodeMirror instance?


I'm using Codemirror v3.16 and I'm trying to figure out how I can kill an instance of my codemirror? Basically codemirror fires when a textarea opens in a modal on my page. Closing this modal, I need to kill the instance, else when I reopen the modal, I get two textareas.

Can anyone help?


Solution

  • If the CodeMirror instance was created with CodeMirror.fromTextArea, you can use its toTextArea method to copy the current contents to its "mirrored" text area and remove the instance.

    Assuming your CM instance has the id of "CMEditor":

    CM = document.getElementById('CMEditor');
    CM.CodeMirror.toTextArea();
    

    Alternately, you could instantiate the CM instance outside of the modal, and just hide and show it when the modal opens.