• What we have here, is a failure to communicate…

CKEditor – Adding Content – $FC()

I wanted to be able to insert content into a CKEditor instance and on top of that, I wanted to concatenate with any existing content. Since I just had a little trouble figuring this out, so I decided to post it here for others to see.

The Solution

Because we use PrototypeJS, we have a helper function that allows us to get the value (only, no set) of an HTML element using the element ID:

$F(element)

So we wrote a helper function that does both get and set content of the CKEditor instance with one function:

/**
 * @param {string} $sElementID
 * @param {string=} $sValue
 * @return {string}
 */
function $FC($sElementID, $sValue){

	$sValue = DefaultArg($sValue, null);

	if($sValue !== null) CKEDITOR.instances[$sElementID].setData($sValue);

	return CKEDITOR.instances[$sElementID].getData();

}

If $sValue not set to null, the instance is updated, but either way, the current value is returned.

To make this all work together:

<button onclick="$FC('txtNote', $FC('txtNote') + 'My New Content');">Add Content</button>

I love it when code is easy and just works!