Categories
JavaScript Answers

How to Get the Entered Value From a TinyMCE Text Area?

Spread the love

To get the entered value from a TinyMCE text area, we can use the tinyMCE.activeEditor.getContent() method.

For instance, if we have the following HTML:

<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>

<form method="post">
  <textarea id="mytextarea">Hello, World!</textarea>
</form>

to add the TinyMCE script and text area, then we can get the value when the editor is initialized by writing:

tinymce.init({
  selector: '#mytextarea',
  setup(editor) {
    editor.on('init', () => {
      console.log(tinyMCE.activeEditor.getContent())
    })
  }
});

We call tinymce.init to create the text editor.

selector is the textarea element that we want to convert to a TinyMCE editor.

Then we have the setup method that calls editor.on with 'init' to watch when the editor is initialized.

Then callback will run when it’s initialized, so we can get the input value of the editor when the editor is initialized with tinyMCE.activeEditor.getContent() .

Therefore, we should see '<p>Hello, World!</p>’ logged.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.