Categories
Code Snippets jQuery

Replace Smart Quotes With Straight Quotes

I created a web app that uses ckEditor as the rich text editor. It works perfectly except when you copy and paste text that includes smart quotes and other special symbols. CkEditor converts the smart quotes to HTML entities, which would be fine in a normal situation. The Html entities caused a problem when it was time to write the iTunes XML feed. So what I ended up doing is replacing all the html entities with the correct character as soon as I got the content from ckEditor. The code I used to replace smart quotes with straight quotes is below.

function cleanstring(dirty){
	var smartchr = [ "’","‘","“","”","–","—","…", " ", '„', '‚' , '«','»', '‹', '›'];
	var correctchr = ["'", "'", '"', '"', '-', '-', '...', '', '"', "'", '"', '"', "'", "'"];

	var thestring = dirty;
	var regex; 
  	for (var i = 0; i < smartchr.length; i++) {
    	regex = new RegExp(smartchr[i], "g");
    	thestring = thestring.replace(regex, correctchr[i]);
	}	


	return thestring;
}
Categories
jQuery

Get Content from CkEditor Using JQuery

In a recent project I used the CkEditor to add a WYSIWYG editor to the private control panel. It was easy to add the editor to the page, but I struggled to get content from CkEditor using jQuery.

Normally I would use the following code to get content on submit. But that does not work when using CkEditor .

var myContent = jQuery('#my_content').val();

The above code works as long as it is a textarea. Once you add the CkEditor class to the textarea the textarea is replaced with a rich text editor.

#my_content

is the id of the textarea field on the form. For example

< textarea id="my_content" class="ckeditor" >

Now to get the content on submit you have to use the following

var myContent = CKEDITOR.instances.my_content.getData()