Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract HTML content from TinyMCE Editor

I'm a beginner on Javascript/TinyMCE and I try to understand how is it possible to get the HTML content from the editor, and show it with a simple alert() function.

I've this minimalist config on my HTML page :

<div id="tiny">
<script type="text/javascript">
tinyMCE.init({
        // General options
        mode : "specific_textareas",
        editor_selector : "mceEditor"
});
</script>
</div>

<form method="post" action="somepage">
        <textarea id="myarea1" class="mceEditor">This will be an editor.</textarea>
</form>

On the TinyMCE Website, They explained that i have to use this :

// Get the HTML contents of the currently active editor
console.debug(tinyMCE.activeEditor.getContent());

And here too

tinymce.activeEditor.getContent()

I don't know why it doesn't work

Someone have an idea ?

like image 322
Ikes Avatar asked Dec 24 '12 15:12

Ikes


People also ask

How do I get HTML from TinyMCE editor?

The TinyMCE getContent and setContent methods You can do this using the getContent() API method. Let's say you have initialized the editor on a textarea with id=”myTextarea”. This will return the content in the editor marked up as HTML.

How do I get the content of TinyMCE editor in jQuery?

You can use the activeEditor for that, or if (for some reason) you have the original element that created the editor in a jQuery object, you can use that jQuery object to get the id of the original element and use that in order to get the content of the TinyMCE (using the TinyMCE editor).


1 Answers

I don't know why it doesn't work

It's not working because

console.debug(tinyMCE.activeEditor.getContent());

tinymce.activeEditor.getContent();

these statements are not being executed.

Try to follow this FIDDLE ....

tinyMCE.init({
        // General options
        mode : "specific_textareas",
        editor_selector : "mceEditor"
});

Function for getting content ....

function get_editor_content() {
  // Get the HTML contents of the currently active editor
  console.debug(tinyMCE.activeEditor.getContent());
  //method1 getting the content of the active editor
  alert(tinyMCE.activeEditor.getContent());
  //method2 getting the content by id of a particular textarea
  alert(tinyMCE.get('myarea1').getContent());
}

Get the content of editor on button click ...

<button onclick="get_editor_content()">Get content</button> 
like image 103
yb007 Avatar answered Sep 29 '22 12:09

yb007