How about something like this:
var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("code"), { lineNumbers: true, viewportMargin: 50 }); var height; if(myCodeMirror.lineCount() > 5) { height = 50; } else { height = 20 * myCodeMirror.lineCount(); } myCodeMirror.setSize(500, height);
This is an example. You can handle the event to dynamically resize your CodeMirror when changing strings.
source share