11
votes

I would like to reset all the contents of the Codemirror editor. Ideally, this should clear the textarea, remove all marks and clear the history.

What's the best way to achieve this?

4
destroy the instance and create a new one?Eliran Malka

4 Answers

20
votes
cm.setValue("");
cm.clearHistory();
// cm.clearGutter("gutterId"); if you have gutters
3
votes

Also, doing cm.setValue(newValue) just sets content of the editor without deleting the CM instance. Doc

1
votes

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

1
votes
var codemirror = $('#YOUR_TEXTAREA_ID').nextAll('.CodeMirror')[0].CodeMirror;
codemirror.getDoc().setValue("");