0

I managed to get the #content field html with this jquery call:

jQuery("#content").html()

This seems to be failing though on "Add New Post" pages. Any tips on how to get the contents of the #content div?

Cheers

2
  • It seems there is no div#content within the Add New Post page!
    – 2hamed
    Commented Nov 16, 2011 at 13:49
  • I can see it <div id='editorcontainer'><textarea rows='20' class='theEditor' cols='40' name='content' tabindex='2' id='content'></textarea></div>
    – giorgio79
    Commented Nov 16, 2011 at 13:55

2 Answers 2

0

The div #editorcontainer contains a regular textarea #content and an iframe #content_ifr.

The #content is filled with the saved contents of the post when the page loads. This means that any live edits of the content won't be returned when calling jQuery("#content").html(). For the same reason, you get an empty string in the case of a new post.

Where you really need to look for the live updated contents is in the iframe, which is used by the WYSIWYG editor. Here's how:

jQuery('#content_ifr').contents().find('#tinymce').html();

Also see: jquery/javascript: accessing contents of an iframe.

2
  • Much appreciated. Noticed this works as well tinyMCE.activeEditor.getContent(content) Wonder if there is a universal solution, as there is a CK Editor plugin as well wordpress.org/extend/plugins/ckeditor-for-wordpress
    – giorgio79
    Commented Nov 16, 2011 at 14:48
  • @giorgio79: doesn't work for me. Returns NULL. Also if I try tinyMCE.activeEditor.getContent(content) it gets an error saying activeEditor is null do i have to enqueue any script?
    – Sisir
    Commented Dec 15, 2012 at 7:24
0

my code works fine in this form:

tinyMCE.triggerSave();
txt = $('#myTextarea')
    .contents()
    .find('#tinymce')
    .html()
    .replace('&lt;br data-mce-bogus="1"&gt;', '')
;
if (console && console.log) console.log(txt);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.