views:

251

answers:

2

I have a HTML form with a few fields and one of them is textarea managed by CKEditor. When the user wants to submit the form, I want to check whether he entered values in all the fields.

I know how I can check if CKEditor control has anything in it, but those could be "empty" HTML tags, without any text in it. How do I check for text?

On server side, I'm using something like PHP's trim(strip_tags($content)), so I'd like to have the same in JavaScript. Solution using jQuery would also be usable.

+6  A: 

This will work:

$("#editorContainer iframe").contents().find("body").text();

That will contain only the text, and none of the html tags.

UPDATE

It definitely works on the CKEditor demo page. Use Firefox and Firebug, go to the Firebug console, and type in:

$("#demoInside iframe").contents().find("body").text();

The console will print out the text in the editor, with no html tags. Make sure you have the selector correct in your particular application. You can test your selector like this:

$("#demoInside iframe").contents().find("body").length;

That should equal 1. If it's 0, your selector is wrong.

UPDATE 2

Again, my code is still correct, and it still works on that page. You just need the right selector. On the page you linked to, it is a <span> with id cke_editor1. That particular page does not make use of jQuery, so it requires some additional work to prove this sample works. Install FireQuery, "jqueryify" the page, and then do this in the Firebug console (note you have to use jQuery and not $. That's how FireQuery works).

jQuery("#cke_editor1 iframe").contents().find("body").text();

In short, make sure you have the right selector to get to your iframe. Whether you create your CKEditor from a <div> or a <textarea> is not important. As long as you can select the <iframe> that CKEditor injects into the DOM, you can use .contents().find("body").text() to get the text of that iframe. Have you tested your jquery selector to see if .length == 1?

Samuel Meacham
Doesn't seem to work, I get an empty string even if I have some text typed into control. I tried replacing #editorContainer with #editor1 as that is the ID of textarea, but that does not work either.
Milan Babuškov
Demo replaces DIV, while my code replaces TEXTAREA. Could you tell me which selector to use on CKEditor sample page: http://nightly.ckeditor.com/latest/ckeditor/_samples/replacebyclass.html ? This would help me determine what should I use in my code. Thanks.
Milan Babuškov
I know this question is old, but I'm curious what would select the val() as well, because the .text() just pulls the the text but none of the html or formatting.
Trip
@Trip, probably .html()
Samuel Meacham
A: 

We use prototype, with that library and the following addition:

Element.addMethods({  
  getInnerText: function(element) {
    element = $(element);
    return element.innerText && !window.opera ? element.innerText
      : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
  }
});

(Thanks to Tobie Langel)

I was able to use the following function to determine whether any actual text was inside CKEditor:

function editorEmpty(instanceName){

    var ele = (new Element('div')).update(CKEDITOR.instances[instanceName].getData()); 

    return (ele.getInnerText() == '' || innerText.search(/^(&nbsp;)+$/i) == 0);
}

Note the test for &nbsp; as well - often when the editor appears empty, it actually contains something like: <p>&nbsp;</p>.

Michael Robinson