How to set a value of CodeMirror editor by javascript command? I just need to set a text to it. But how?.. Thanks a lot for the help!
Once you import CodeMirror via <script> tag, you get a CodeMirror global that you can use to create a new CodeMirror instance. Just point it at a <div> and CodeMirror does the rest. CodeMirror(document. querySelector('#my-div'), { lineNumbers: true, tabSize: 2, value: 'console.
If you don't want to kill the CodeMirror instance, just change the entire document holding the text, data on markers etc. This can be done by calling cm. swapDoc(doc: CodeMirror. Doc) .
// 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.
It is explained in the manual, section Programming API:
setValue(string)
: Set the editor content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With