monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

Ability to grab contents of editor as text.

Open aarinsmith opened this issue 10 years ago • 3 comments

Is this a possibility? Say I wanted to include the contents of the editor as part of a form and submit the entered text? Codemirror supports this by allowing you to bind the editor to a textarea. Binding it like this is not really necessary in my specific situation but an easy call to grab the contents of the editor as text would be helpful. Is this possible?

aarinsmith avatar Jun 22 '16 22:06 aarinsmith

Yes, Try this in the playground

var editor = monaco.editor.create(document.getElementById("container"), {
    value: "function hello() {\n\talert('Hello world!');\n}",
    language: "javascript"
});
console.log(editor.getValue())

MohammadYounes avatar Jun 22 '16 22:06 MohammadYounes

Awesome thanks for the quick response. I had assumed there was a simple solution to this but was for some reason unable to find it.

aarinsmith avatar Jun 22 '16 23:06 aarinsmith

So, it's not OK to put the editor inside a <form> and expect to get the full content to be send to the server. The value should be retrieved with editor.getValue() just before submit the form.

M-Yankov avatar Apr 13 '19 16:04 M-Yankov