Search code examples
javascriptjquerycodemirror

CodeMirror: textarea setValue() pro


I was trying to apply codemirror to my textarea after ajax query is done, but always get blank textarea (text is loaded, but hidden, so textarea is blank). When I right-click on the textarea and simply open Chrome DevTools text magically appears, same problem in all browsers I tried - Mozilla (FireBug), Opera

Here is my codemirror code:

CodeMirror.fromTextArea(result).setValue(JSON.stringify(data,null,'\t'));

CodeMirror version 3.21


Solution

  • I suspect the textarea is hidden, or some other factor is preventing the editor from rending when it is initialized. CodeMirror can't properly build up its display when hidde, so you have to call its refresh method when you unhide it. A window resize (as triggered by opening devtools) will cause it to refresh automatically.