Editorial
How to get content and set content in TinyMCE

Initiuitivity dictates that it should be easy to get or set the text using a standard textarea, but TinyMCE provide some more flexible methods to do it.

Some of the most common questions we are asked by developers, when they’re integrating TinyMCE with their apps for the first time, are about how to get content and set content in the editor.

The TinyMCE getContent and setContent methods

getContent()

Once a user has entered content in the editor, you will probably want to save the content to a database somewhere, in which case you’ll need to get the content first. You can do this using the getContent() method from the TinyMCE API.

Let’s say you have initialized the editor on a textarea with id=”myTextarea”. First access the editor using that same id, then call getContent().

See an example

Check out our previous article where we save to and load from our rich text editor using JavaScript and localStorage. After the TinyMCE editor is added to the initial example, the save and load functions are updated to access the editor content using these methods.

How to set TinyMCE content on initialization

Often you will want the editor populated with content as soon as it’s initialized. In this case, you can use the setup option in the editor config.

How to get content in TinyMCE without any HTML tags

Depending on your use case, sometimes you might want to get the TinyMCE content without the HTML markup. In this case, when calling getContent, you can pass in a parameter to indicate that, instead of returning HTML, you want the result in plaintext.

What next?

Is there anything else you’d like to see here in this article? Contact me on Twitter and let me know.

While we’re talking about getting and setting TinyMCE content, you might also be interested to know...

Not already using TinyMCE on the cloud? Get a free API Key and try it out. It’s super easy to set up. You also get a 30 day trial of the full suite of premium plugins.