How to get the value of Codemirror textarea

Nitin Kabra picture Nitin Kabra · Apr 23, 2012 · Viewed 52.3k times · Source

I am using Codemirror's plugin for textarea but I am not able to retrieve the value of textarea.

Code:

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    matchBrackets: true,
    mode: "text/x-csrc"
  });


function showCode()
{
    var text = editor.mirror.getCode();
    alert(text);
}

It is showing the error:

editor.getCode() is not a function.

Answer

Eric Leschinski picture Eric Leschinski · Apr 23, 2012

Try using getValue() instead of getCode().

Pass in an optional argument into getValue(separator) to specify the string to be used to separate lines (the default is \n).