Clear CKeditor form after submission

Posted on 2011-02-20
Last Modified: 2012-06-27
I am creating a CKeditor instance for some content management and after submission would like to clear the input field automatically.
I am posting the form contents using jQuery if this makes and difference.
Question by:Lmillard
  • 5
  • 3
  • 2
LVL 20

Expert Comment

by:Mark Brady
ID: 34938201
Does your form use php session() ? If so, you can kill the session like this:


Run that line in php script to destroy session and data inside session so won't show up in your form anymore.

If not:

Javascript will get rid of the values. Just rename the variables in my function and add more to suit your form.
Place this in between the <head> and </head> tags on your page.

<script language="javascript">
function  clearForm()
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('address').value = '';
document.getElementById('phone').value = '';
// keep going untill all your form input fields are empty. Replace the values inside the quote marks with the "name" value on each of your form elements.

To run the script, you could add some code to the end of your submit button or add it to some other process. Here's an example.

<input type='button' onclick="document.forms.MyForm.submit(),clearForm()" />

Although, this would work, it is not a good idea to put the code on your submit button in case there is an error with data in the form you will need to alert the user and have them fix it. You don't want them to have to refill out the form each time!

Hope this helps. If not, can you be more specific on what fields you have on your form and how you are submitting it. Perhaps post your form code here or attach it.

Author Comment

ID: 34939065
Thanks for coming back so quickly.
I am using Coldfusion so no access via the php method.

I have already tried clearing the input using traditional javascript getElementById but this does not seem to clear the field, not sure why. It clears all fields up to the text area that is being used by CK but not the text area itself.
I am posting the code below for all to see.

<form id="theForm">
<input type="hidden" name="NewRec" value="#NewRec#" />
<input type="hidden" name="BlogId" value="#Url.BlogId#" />

<cfif Not NewRec>
<input type="hidden" name="bUrl" value="#bUrl#" />

<table align="center" border="0" width="100%">
    <td>Enter Post Title</td>
    <td width="7px"><img src="#Application.BoPath#Assets/Img/backoffice/mand.png" alt="" /></td>
    <td><input type="text" name="BlogTitle" id="BlogTitle" value="#BlogTitle#" style="width:515px" /></td>
    <td>Select a Catagory</td>
    <select name="BlogCat" id="blogCat">
    <cfif ThisBlogCat.RecordCount is 0>
    <option value=""></option>
    <option value="#ThisBlogCat.CatId#">#ThisBlogCat.CatDesc#</option>
    <cfloop query="AllBlogCat">
    <option value="#AllBlogCat.CatId#">#AllBlogCat.CatDesc#</option>
    <td>Posted By</td>
    <td ><img src="#Application.BoPath#Assets/Img/backoffice/mand.png" alt="" /></td>
    <input type="text" name="UsrSynom" size="20" id="UsrSynom" value="#UsrSynom#" />
    Display on Live Website?
    <select name="Status">
    <cfswitch expression="#Status#">
    <cfcase value="1">
    <option value="1">Show on live site</option>
    <option value="0">Hide from live site</option>
    <option value="0">Hide from live site</option>
    <option value="1">Show on live site</option>
    <td>Website Url</td>
    <cfif NewRec>
    <td><img src="#Application.BoPath#Assets/Img/backoffice/mand.png" alt="" /></td>
    <td><input type="text" name="bUrl" size="50" id="bUrl" value="#bUrl#" style="width:315px" /></td>
	<td><a href="#FullPostPath#" target="_blank" title="View link in new tab">#Application.BoPath#blog/posts/#bUrl#.cfm</a></td>

<div style="padding-top:10px;">
		<textarea cols="100" id="editor1" name="BlogContent" rows="10">#blogContent#</textarea>

		<script type="text/javascript">
			// Replace the <textarea id="editor1"> with an CKEditor instance.
			CKEDITOR.replace( 'editor1',
					toolbar :
					uiColor : '##eee',
    			    filebrowserUploadUrl : '../fileUploadMulti/ckUpload.cfm'

		<div id="eMessage">

<div style="width:100%; padding-top:10px; text-align:right">
<cfif NewRec>
    <input type="submit" name="submit" class="button" value="Create Post" />
    <input type="submit" name="submit" class="button" value="Apply Changes" />

<div id="searchresults" style="text-align:left"></div>



        <cfinvoke component="#Application.CompPath#Blog" method="Create"

        <img src="#Application.BoPath#Assets/Img/backoffice/bulletpoint.png" alt="BulletPoint" />
        <p class="header2">Thank you, your blog post has been created</p>
            document.getElementById('BlogTitle').value = '';
            document.getElementById('BlogTitle').value = '';
			document.getElementById('blogCat').value = '';
			document.getElementById('bUrl').value = '';

Open in new window

LVL 20

Expert Comment

by:Mark Brady
ID: 34939083
For the textarea it should be

var tArea = document.getElementById('editor1');
tArea .value = '';

That should wipe it out.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Author Comment

ID: 34939116
Not working for some reason, I'm no expert with js but I'm totally sure I have it all typed correctly and in the right place as all the fields are cleared other than the CKeditored text area.

Expert Comment

ID: 34941972
If you are using COLDFUSION wich version do u have? If 8 or higher its simple - just one tag for an FCK Editor:

<cfform name="form1">
  <cftextarea name="textarea1" height="400" width="480" richtext="yes" toolbar="Basic" skin="silver" toolbaronfocus="yes" value="#YOUR_VALUE#"></cftextarea>

Here’s how you can set the value of the editor using client side JavaScript.

This is not a documented feature though.

var editorinstance = ColdFusion.objectCache['nameoftextarea'];

var editor = FCKeditorAPI.GetInstance(editorinstance.richtextid);


If you have Version 7 or earlier - let me know because i have an component for U

Author Comment

ID: 34942032
I wish I had know that CF has the ability to create the form using cfforms but unfortunately I am too far in to be able to switch to this method but can consider this for future versions on my application.
I am using CF9 so should have the full functionality in place to do this.
I am using the newer CK editor, I presume its all the same as the older FCK.

Expert Comment

ID: 34942103
theres no problem if you switch to cfform instead of a form tag.  Your can keep your old form elements! just change your tag <form .... to <cfform ...... (and the end tag too, of course)
then make a textarea like <cftexarea and you'll see It will do everthing for you.

Tell me if it works for You.

Author Comment

ID: 34942181
this code runs in many places in applications that are distributed so I really do need to go with a non cfform option at the moment but will definitely be considering a measure code change across the board in the future

Accepted Solution

dagaz_de earned 500 total points
ID: 34942482
Ok then you must use a Command to reset the value like this:

are you using CKEDITOR 3.0?

then you must use the setData method;

CKEDITOR.instances.editor1.setData( '' );

more information u can get Here:


Author Closing Comment

ID: 34942984
Thanks so much that has worked perfectly. Also thanks for the CF tip, I will be moving over to this asap!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question