How can I get the content of CKEditor using JQuery?
JqueryAjaxCkeditorJquery Problem Overview
I'm using CKEditor. I am saving the form values with ajax using page methods.
But the content of CKEditor value cannot be saving into the table.
I dont postback the page.
What can I do for that?
Jquery Solutions
Solution 1 - Jquery
use the CKEditor.editor.getData() call on the instance. That is to say:
HTML
<textarea id="my-editor">
<input id="send" type="button" value="Send">
JS for CKEditor 4.0.x
$('#send').click(function() {
var value = CKEDITOR.instances['DOM-ID-HERE'].getData()
// send your ajax request with value
// profit!
});
JS for CKEditor 3.6.x
var editor = CKEDITOR.editor.replace('my-editor');
$('#send').click(function() {
var value = editor.getData();
// send your ajax request with value
// profit!
});
Solution 2 - Jquery
If you don't hold a reference to the editor, as in Aeon's answer, you can also use the form:
var value = CKEDITOR.instances['my-editor'].getData();
Solution 3 - Jquery
var value = CKEDITOR.instances['YourInstanceName'].getData()
alert( value);
Replace YourInstanceName
with the name of your instance and you will get the desired results.
Solution 4 - Jquery
I was having issues with the getData()
not working every time especially when dealing with live ajax.
Was able to get around it by running:
for(var instanceName in CKEDITOR.instances){
CKEDITOR.instances[instanceName].updateElement();
}
Then use jquery to get the value from the textarea.
Solution 5 - Jquery
Now that jQuery adapter exists, this answer needs to be updated:
Say you initiated the editor with $('.ckeditor').ckeditor(opt)
then you get the value with $('.ckeditor').val()
Solution 6 - Jquery
To get data of ckeditor, you need to get ckeditor instance
HTML code:
<textarea class="form-control" id="reply_mail_msg" name="message" rows="3" data-form-field="Message" placeholder="" autofocus="" style="display: none;"></textarea>
Javascript:
var ck_ed = CKEDITOR.instances.reply_mail_msg.getData();
Solution 7 - Jquery
Thanks to John Magnolia. This is my postForm function that I am using in my Symfony projects and it is fine now to work with CK Editor.
function postForm($form, callback)
{
// Get all form values
var values = {};
var fields = {};
for(var instanceName in CKEDITOR.instances){
CKEDITOR.instances[instanceName].updateElement();
}
$.each($form.serializeArray(), function(i, field) {
values[field.name] = field.value;
});
// Throw the form values to the server!
$.ajax({
type : $form.attr('method'),
url : $form.attr('action'),
data : values,
success : function(data) {
callback( data );
}
});
Solution 8 - Jquery
you can retreive data like this :
<script>
var data = CKEDITOR.instances.editor1.getData();
// Your code to save "data", usually through Ajax.
</script>
reference : http://docs.ckeditor.com/#!/guide/dev_savedata
Solution 9 - Jquery
version 4.8.0
$('textarea').data('ckeditorInstance').getData();
Solution 10 - Jquery
i add ckEditor by adding DLL in toolBox.
html code:
<CKEditor:CKEditorControl ID="editor1" runat="server"
BasePath="ckeditor" ContentsCss="ckeditor/contents.css"
Height="250px"
TemplatesFiles="ckeditor/themes/default/theme.js" FilebrowserBrowseUrl="ckeditor/plugins/FileManager/index.html"
FilebrowserFlashBrowseUrl="ckeditor/plugins/FileManager/index.html" FilebrowserFlashUploadUrl="ckeditor/plugins/FileManager/index.html"
FilebrowserImageBrowseLinkUrl="ckeditor/plugins/FileManager/index.html" FilebrowserImageBrowseUrl="ckeditor/plugins/FileManager/index.html"
FilebrowserImageUploadUrl="ckeditor/plugins/FileManager/index.html"
FilebrowserUploadUrl="ckeditor/plugins/FileManager/index.html" BackColor="#FF0066"
DialogButtonsOrder="Rtl"
FontNames="B Yekan;B Yekan,tahoma;Arial/Arial, Helvetica, sans-serif; Comic Sans MS/Comic Sans MS, cursive; Courier New/Courier New, Courier, monospace; Georgia/Georgia, serif; Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif; Tahoma/Tahoma, Geneva, sans-serif; Times New Roman/Times New Roman, Times, serif; Trebuchet MS/Trebuchet MS, Helvetica, sans-serif; Verdana/Verdana, Geneva, sans-serif"
ResizeDir="Vertical" ResizeMinHeight="350" UIColor="#CACACA">dhd fdh</CKEditor:CKEditorControl>
for get data of it.
jquery:
var editor = $('textarea iframe html body').html();
alert(editor);
Solution 11 - Jquery
I think it will be better, just serialize your form by jquery and cheers...
<form id="ajxForm">
<!-- input elments here -->
<textarea id="ck-editor" name="ck-editor" required></textarea>
<input name="text" id="text" type="text" required>
<form>
and In javascript section
CKEDITOR.replace('ck-editor', {
extraPlugins: 'sourcedialog',
removePlugins: 'sourcearea'
});
$("form#ajxForm").submit(function(e) {
e.preventDefault();
var data = $(this).serialize();
if (data != '') {
$.ajax({
url: 'post.php',
cache: false,
type: 'POST',
data: data,
success: function(e) {
setTimeout(function() {
alert(e);
}, 6500);
}
});
}
return;
});
Solution 12 - Jquery
version 4.6
CKEDITOR.instances.editor.getData()
Solution 13 - Jquery
Easy way to get the text inside of the editor or the length of it :)
var editorText = CKEDITOR.instances['<%= your_editor.ClientID %>'].getData();
alert(editorText);
var editorTextLength = CKEDITOR.instances['<%= your_editor.ClientID %>'].getData().length;
alert(editorTextLength);
Solution 14 - Jquery
Using Pure Vanilla Javascript / Jquery or in any javascript library :
If you have Ckeditor loaded in below text-area:
<textarea name="editor1" id="editor1"></textarea>
Then you can get content inside textarea as below:
var txtNotes = document.getElementsByClassName('ck-content')[0].innerHTML;
Solution 15 - Jquery
First of all you should include ckeditor and jquery connector script in your page,
then create a textarea
<textarea name="content" class="editor" id="ms_editor"></textarea>
attach ckeditor to the text area, in my project I use something like this:
$('textarea.editor').ckeditor(function() {
}, { toolbar : [
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
['Undo','Redo'],
['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Link','Unlink','Anchor', 'Image', 'Smiley'],
['Table','HorizontalRule','SpecialChar'],
['Styles','BGColor']
], toolbarCanCollapse:false, height: '300px', scayt_sLang: 'pt_PT', uiColor : '#EBEBEB' } );
on submit get the content using:
var content = $( 'textarea.editor' ).val();
That's it! :)