How to get a reference to a CodeMirror instance

If you want to access a CodeMirror instance, perhaps because it was created programmatically, or by a module you don’t control, you can access it like this:

//Get a reference to the CodeMirror editor
var editor = $('.CodeMirror')[0].CodeMirror;

//You can then use it as you wish
editor.setValue('lorem ipsum yada yada');
editor.replaceSelection('this is a test');

In fact, the CodeMirror wrapper element (which has the CodeMirror class) has a CodeMirror attribute which you can use to access the editor’s options and methods.

This can be pretty useful if you use ui.codemirror from Angular UI

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax