0

I am using CKEditor in my Rails project

The view looks like this:

<%= cktext_area_tag "editor_name","", :style=>"width:115px; height:19px; border:#d1d1d1 1px solid;  background:#cfcfcf" %>

There is no content in the editor when it is loaded. But while checking the console I could see this:

    > var editor_val = CKEDITOR.instances.editor_name.document.getBody().getChild(0).getText() ;
    < undefined
    > editor_val
    < "
      "
    > editor_val.length
    < 1

I want the CKEditor default value to be blank. How is it possible?

webster
  • 3,902
  • 6
  • 37
  • 59
  • 1
    The first *undefined* is corrent, the return value is assigned to variable and not written to output (ruby does it, but chrome console does not!). For the next part we need to see your code how you initialize editor and textareas.. And offcourse you can always use Trim function http://www.w3schools.com/jsref/jsref_trim_string.asp editor_val.trim().length – Jan Strnádek Apr 14 '15 at 09:47

1 Answers1

0

CKEditor is never truly empty, if you think about DOM, not the data:

CKEDITOR.instances.editor.setData( '' );
CKEDITOR.instances.editor_name.editable().getText().length;
1
CKEDITOR.instances.editor_name.editable().getText().charCodeAt( 0 );
10
CKEDITOR.instances.editor_name.editable().getHtml();
"<p><br></p>" 
CKEDITOR.instances.editor_name.getData().length;
0

That "persistent" white character (10) (do not confuse with with keyCode) is a new line, hardly a reminiscence of <br> which is to enable editing in CKEditor and may differ depending on browser/OS. It's an internal starting point for CKEditor to display the caret and the initial collapsed selection which means you should not rely on it.

Since the only safe method of obtaining CKEditor content is:

CKEDITOR.instances.editor_name.getData();

I would suggest stripping tags on the server-side or CKEDITOR.tools.trim() if you really want to follow the editable().getText() way.

Community
  • 1
  • 1
oleq
  • 15,697
  • 1
  • 38
  • 65