Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate a tinyMCE editor, if it is blank by appending a string next to it?

I need to validate a form. This form has some dropdowns and tinyMCE editor, I am validating this form by appending the string "Required" after each field if it is blank, However I am unable to validate the tinyMCE editor, if the editor is blank, I tried something like

tinyMCE.get('tinyedotor').getContent();

but no luck.

here is my fiddle

like image 551
Mike Avatar asked Jan 05 '12 20:01

Mike


People also ask

How do I know if my TinyMCE is empty?

You can do this to check if the content is empty without parsing html: var content = tinymce. get('tinymceEditor'). getContent({format: 'text'}); if($.

How do I reinitialize TinyMCE editor?

Use tinymce. remove() method to remove TinyMCE editor from the HTML element and again call tinymce. init() on the selector to reinitialize.

How do you get content on TinyMCE?

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 you make TinyMCE editor readonly?

Use the checkbox to toggle between the "design" and "readonly" modes.


1 Answers

getContent() should work just fine. Your fiddle doesn't contain the form validation code for the editor value, which is quite crucial here. Try this:

var editorContent = tinyMCE.get('tinyeditor').getContent();
if (editorContent == '')
{
    // Editor empty
}
else
{
    // Editor contains a value
}

Forked fiddle

Also note you've declared multiple id's for your select drop-down.

Edit: You can get the id of the editor container with the getContainer() method: tinyMCE.get('tinyeditor').getContainer(). Inserting an error message after the editor would then be something like this:

$('<span class="error">Editor empty</span>').insertAfter($(tinyMCE.get('tinyeditor').getContainer()));

This, however, will create a new span each time the user clicks the submit button, so you'll probably want to have an error message container with a unique id and check if the container already exists before inserting it.

Edit 2: Updated fiddle.

like image 175
Viktor Avatar answered Oct 05 '22 10:10

Viktor