For the editor part, the Ghost-Markdown-Editor uses the CodeMirror editor . Thus, to set the value programmatically, we call an instance of CodeMirror and do
editor.setValue(txt);
But how to get this CM instance? It was created by widgets with which the Ghost-Markdown-Editor was created. See the jquery.ghostdown.js file:
$.widget( "b4m.ghostDown", { editor: null, // ... _create: function() { // ... this.editor = CodeMirror.fromTextArea(this.element.find('textarea')[0], { mode: 'markdown', tabMode: 'indent', lineWrapping: true }); } }
Since the widget was created using the jQuery Widget factory, the widget instance is stored inside the .data ("plugin-name") element of the object on which it was used.
Thus, we can access the widget instance and set the editor value as follows:
var ghostdown = $(".editor").data("b4m-ghostDown"); ghostdown.editor.setValue("# Hello, SO");
Or simply
$(".editor").data("b4m-ghostDown").editor.setValue("# Hello, SO");
source share