jQuery validation and TinyMCE

Posted

Just solved a problem where the jQuery validation plugin wasn't playing so nicely with TinyMCE -- the validation plugin was trying to validate the textarea before TinyMCE had a chance to copy the editor contents back to the textarea. I was about to yank TinyMCE out of the page but a little reading through the TinyMCE docs led me to try this:

$('#mySubmitButton').click(function() {
    var content = tinyMCE.activeEditor.getContent(); // get the content
    $('#myTextarea').val(content); // put it in the textarea
});

$('#myForm').validate();
And what do you know, it works. One note: it's important to bind the content replacement to the click event of the submit button, not to the actual form submission, or else the validation may try to run before the content gets copied back to the textarea.

Posted

23 comments

Jan 13, 2009
Justin Yost said...
Thanks for the tip.
Feb 23, 2009
Oliv. said...
your tip has been usefull to me for sending via ajax the content of the textarea,
thanks for writing this clear page !
Mar 12, 2009
Bill said...
Hi Rebecca,

Thank you for posting this! I just spent about an hour trying to figure this out and *almost yanked TinyMCE, too :p

Mar 18, 2009
Rui said...
Thank you...
Great tip! Worked nicelly.
May 11, 2009
Paul said...
Nice work, I spent a while trying to debug this. Now I can have my TinyMCE and use it too.
May 11, 2009
Paul said...
Follow-up - I have a page with several TinyMCE text areas on it. This also works. TinyMCE also supplies a get content by TinyMCE ID method (which is always the name attribute I think) and it will also work:

$('.submit').click(function() {
var content = tinyMCE.get('myContent').getContent(); // get the content
$('#myContentId').val(content); // put it in the textarea
});

You could also use this with the jQuery .each method to update an array of TinyMCE textareas.

May 27, 2009
yann lorber said...
Very good, works perfect !
Jun 05, 2009
steve chan said...
A thanks a trillion! Nice little snippet will come in handy
Jun 08, 2009
Rajesh said...
Hi, Thanks a lot for the fix.
Jun 11, 2009
Joel "Jaykul" Bennett said...
You really should just use tinyMCE.triggerSave()
Jun 11, 2009
Joel "Jaykul" Bennett said...
Here's what we did ... after you include the tinyMCE script and jquery.validate ... you have another scriptblock where you init tinyMCE and the validator like so:

tinyMCE.init({
// whatever ....
});
$(document).ready(function() {
$("input[type='submit']").click(function() {
tinyMCE.triggerSave();
});

// validate signup form on keyup and submit
$("#editEntryForm").validate({
// whatever ...
});
});

Of course, that's still not perfect, because it only works on clicks of the button. Really, the jquery.validate script needs to take a prevalidation script block to handle situations like this...

Jun 25, 2009
Tunde said...
Thanks a million, have been battling this problem for a while now.
Aug 24, 2009
saran said...
hi,
its working well.. thanks lot..
if any one doubt on it.. go through it.

$(document).ready(function(){
$('#submit_curriculum').click(function(){
var content = tinyMCE.activeEditor.getContent();
$('#curriculum_description').val(content);
$("#curriculum").validate();
});

Thanks
saran

Nov 19, 2009
arthur said...
Thank you - I've spent hours trying to figure out why the simple textarea could not be accessed via JQuery - then realized that using TinyMCE hid the value. Your blog entry was very useful.
Nov 24, 2009
suman said...
oh thanks very much.. for this remedy of my prob
Jan 07, 2010
BBonifield said...
Hey Rebecca. I ran into this problem today myself, and low and behold, you're the #1 result for 'jquery.validate tinymce' (wtg!). I was a little against it due to some issues specific to our project, but I was going to go ahead anyway. Well, until I began thinking about how the .click handler won't account for instances where the user just presses enter in the form. I looked at the next few Google results and ran across http://is.gd/5RK8u on the jquery.validate site. It seems to be a cleaner and more reliable way to handle the situation.

The only thing I might add to that is checking the parent form's validate() options to see whether or not you should trigger the .valid() call on blur. Something like...

tinyMCE.init({
mode : "textareas",
theme : "simple",
// update validation status on change
onchange_callback: function(editor) {
tinyMCE.triggerSave();
var $editor = $("#" + editor.id);
if ($editor.closest('form').data('validator').settings.onfocusout !== false) {
$editor.valid();
}
}
});

Feb 25, 2010
shavi said...
Thanks This is very helpful for me..
Apr 20, 2010
Frederik Telleir said...
Thank you so much :-)
May 07, 2010
Manoj Thomas said...
Used this to validate an ExpressionEngine SAEF textarea - thanks!!
Jul 22, 2010
Sean said...
Brilliant - tinyMCE.triggerSave(); worked for me, but thanks for the pointer in the right direction!
Oct 11, 2010
Marcus Sá said...
The tinyMCE.triggerSave(); is a nice tip =D Thanks
Dec 19, 2010
brandon_merritt said...
this suggestion just saved my bacon! you rock, as usual!
Jun 29, 2011
Ravi said...
@Marcus Sá : triggerSave() is great, really liked this.

Leave a comment...