Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of Codemirror textarea

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. 
like image 299
Nitin Kabra Avatar asked Apr 23 '12 17:04

Nitin Kabra


People also ask

How do I get content from CodeMirror?

To get the value of the CodeMirror text editor we need to write some javascript code in the default. js file. $(document).

How do I use CodeMirror in textarea?

This could be used to, for example, replace a textarea with a real editor: var myCodeMirror = CodeMirror(function(elt) { myTextArea. parentNode. replaceChild(elt, myTextArea); }, {value: myTextArea.

How do you find the value of an editor?

Getting/Setting Values: var code = editor. getValue(); editor. setValue("new code here");

How do you use CodeMirror lint?

There's two ways to do this: Find a linter that you can run in the browser, run it on the content, and translate its output into the expected format. Write one from scratch, possibly using the syntax tree kept by the editor.


1 Answers

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

like image 57
Eric Leschinski Avatar answered Sep 23 '22 02:09

Eric Leschinski