Categories
JavaScript Answers

How to Get the Entered Value from the ACE Editor?

Spread the love

To get the entered value from the ACE editor, we can use the getValue method.

And to set the value of the editor, we can use the setValue method.

For instance, we can add the HTML for the editor by writing:

<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js" integrity="sha512-GZ1RIgZaSc8rnco/8CXfRdCpDxRCphenIiZ2ztLy3XQfCbQUSCuk8IudvNHxkRA3oUg6q0qejgN/qqyG1duv5Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<div id="editor" style="height: 200px; width: 200px">
  some text
</div>

Then we can write the following JavaScript code to get and set the editor value:

const editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
const code = editor.getValue();
console.log(code)
editor.setValue("new code here");

We create the editor with:

const editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");

We call ace.edit with the ID of the element to use as the editor element.

Then we call editor.seesion.setMode to set the language mode.

Next, we call editor.getValue to get the value in the editor, which is 'some text‘ .

And the, we call editor.setValue with 'new code here' to replace the existing text with that in the editor.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *