Search code examples
javascriptcodemirror

Get CodeMirror instance


I want to get an instance of CodeMirror (it is binded to a textarea '#code'). From an onclick-event I want to add a value to the current value of the CodeMirror instance. How can this be achieved? From the docs I can't seem to find anything to get an instance and bind it to a loca var in javascript.


Solution

  • Someone just posted an answer but removed it. Nevertheless, it was a working solution. Thanks!

    -- Basically this was his solution:

    // create an instance
    var editor = CodeMirror.fromTextArea('code');
    // store it
    $('#code').data('CodeMirrorInstance', editor);
    // get it
    var myInstance = $('code').data('CodeMirrorInstance');
    // from here on the API functions are available to 'myInstance' again.