javascriptckeditortextareawcmweb-content

javascript - how to show multiple textarea in code editor (one of my attempt is to use CKEditor)


I am using jquery and CKEditor on the project. I want users can write/edit their html codes in CKEditor. When I input multiple textarea tag in the editor, they look fine because they both stay inside of the textarea as follows,

enter image description here

Then I save it, and I confirmed it is saved correctly because I checked the source code in the file.

However, if I open the file in CKEditor again, it is not being shown properly as follows,

enter image description here

The following is the sample codes I used. It is not the completed codes though.

      <textarea id="eidtArea" name="editScriptContent"><!--Load the saved file here (I will skip this part here)--></textarea>
        <script type="text/javascript">
               CKEDITOR.replace( 'eidtArea');
        </script>   

My questions are :


Solution

  • You must encode HTML before printing it out in <textarea>. Otherwise your backend produces this:

    <textarea id="editor">
        <p>Some text: <textarea>foo</textarea></p>
        <p>Some text: <textarea>bar</textarea></p>
    </textarea>
    

    Which will be parsed by browser as:

    <textarea id="editor">
        <p>Some text: <textarea>foo
    </textarea>
    // And here the rest of the content of the editor,
    // but outside the editor's textarea
    

    Therefore, you need to at least replace all < characters with &lt;. E.g. if you use PHP you should pass the content through the htmlspecialchars() function.