Stv
Stv

Reputation: 506

Get tinyMCE content in raw/text format without using active editor

I need to have the raw/text format from an tinyMCE editor.

tinyMCE.activeEditor.getContent({format: 'raw'})

will do the job, but I have 2 editors in the same page (with different ids of course).

I need to count characters on window.onload, but at this moment i have no editor focused.

tinyMCE.get('my_editor').getContent()

can do the job but it doesn't take any parameters to format the output, it only returns html.

How can I get the raw content of a tinyMCE editor by id?

Upvotes: 1

Views: 6937

Answers (1)

Michael Fromin
Michael Fromin

Reputation: 13726

There are likely two issues at play here...

TinyMCE is likely not fully initialized when the window's onload event fires. Trying to use TinyMCE APIs before the editor is initialized simply won't work. Here is a TinyMCE fiddle that shows how to fire code as soon as the editor is loaded using TinyMCE's init callback:

http://fiddle.tinymce.com/5pgaab/2

If you fire code at the correct time, TinyMCE's get() API uses the ID of the textarea to identify a specific editor instance. What is even easier is the init callback in the demo fiddle automatically gets access to the editor instance via a variable TinyMCE passes in to the function so that editor instance can be used to call getContent() without the need to first locate the editor.

EDIT: If you just want the text characters (no HTML tags) you can use a different format parameter in the getContent() call:

editor.getContent({format: 'text'}) 

Here is an updated example: http://fiddle.tinymce.com/5pgaab/3

Upvotes: 1

Related Questions