How to disable CKeditor with jQuery
I am using CKEditor in my web application and need to disable/enable the editor from javascript. I know that there is an option called readOnly but I don't know how to set it from jQuery.
Does anybody know how to disable and e开发者_如何学Pythonnable back the CKEditor using jQuery please?
The easiest way:
To disable CKEditor:
CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(true);
To enable CKEditor:
CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(false);
I am using it in codeigniter. In the view file I am using it like this:
<script type="text/javascript">
$(document).ready(function() {
CKEDITOR.config.readOnly = true;
});
</script>
I assume you are using the CKE's jQuery adapter, so you can use .ckeditorGet()
jQuery function to quickly access the CKEditor's API.
To switch a running editor instance in read only mode, use the setReadOnly
editor function:
$( _your_textarea_ ).ckeditorGet().setReadOnly();
To switch it back into writable use:
.setReadOnly( false );
You can also use readOnly
configuration value, when you are firing up your editor to start it in readonly mode right away:
$( _your_textarea_ ).ckeditor({
readOnly: true
});
Assuming you have included jQuery adapter following code should make it readonly. You can take the jQuery adapter from the example if not yet included.
<div class="wrapper">
<form id="myfrm" name="myfrm" class="myfrm" action="" method="post">
<textarea id="myeditor" name="myeditor"></textarea>
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
</div>
and the js
$(document).ready(function(e) {
var myeditor = $('#myeditor');
myeditor.ckeditor();
myeditor.ckeditorGet().config.resize_enabled = false;
myeditor.ckeditorGet().config.height = 200;
myeditor.ckeditorGet().config.readOnly = true;
});
To enable or disable a ckeditor based on your selection of a select box you'd have to make a change event like this
$(document).ready(function(){
//put ckeditor initialization (the above) here.
$('#myselect').change(function(){
var x = $(this);
if(x.val()=='enable'){
myeditor.removeAttr('disabled');
}else if(x.val()=='disable'){
myeditor.attr('disabled','disabled');
}
myeditor.ckeditorGet().destroy();
myeditor.ckeditor();
});
});
What we are doing above is setting the original element to have attribute disabled="disabled"
and reloading ckeditor after destroying the current instance. Check the JSFiddle Example 2.
JSFiddle Example to reflect OP's query
I think you haven't read that documentation properly...it says that Ckeditor has some instance name, and with that instance name in the tag of the page set InstanceNameOfCkEditor.config.readOnly = true;
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setReadOnly
From: cksource forums
// Temporary workaround for providing editor 'read-only' toggling functionality.
( function()
{
var cancelEvent = function( evt )
{
evt.cancel();
};
CKEDITOR.editor.prototype.readOnly = function( isReadOnly )
{
// Turn off contentEditable.
this.document.$.body.disabled = isReadOnly;
CKEDITOR.env.ie ? this.document.$.body.contentEditable = !isReadOnly
: this.document.$.designMode = isReadOnly ? "off" : "on";
// Prevent key handling.
this[ isReadOnly ? 'on' : 'removeListener' ]( 'key', cancelEvent, null, null, 0 );
this[ isReadOnly ? 'on' : 'removeListener' ]( 'selectionChange', cancelEvent, null, null, 0 );
// Disable all commands in wysiwyg mode.
var command,
commands = this._.commands,
mode = this.mode;
for ( var name in commands )
{
command = commands[ name ];
isReadOnly ? command.disable() : command[ command.modes[ mode ] ? 'enable' : 'disable' ]();
this[ isReadOnly ? 'on' : 'removeListener' ]( 'state', cancelEvent, null, null, 0 );
}
}
} )();
And usage:
// Turn CKEditor into 'ready-only' mode or vice versa.
CKEDITOR.instances.editor1.readOnly( true );
CKEDITOR.instances.editor1.readOnly( false );
I add a disabled to the textarea which seems to be the simplest way.
<div class="col-md-12">
<h3>HTML CONTENT</h3>
<textarea name="editor1" disabled></textarea>
<script>
CKEDITOR.replace('editor1');
</script>
</div>
In our project we are hiding the CKEditor and using a ReadOnlyText Area to display existing text. Hope this helps.
you try this is the best solution
var existingEditor = CKEDITOR.instances && CKEDITOR.instances[element.id];
try {
if (existingEditor && typeof(existingEditor) !== "undefined" && typeof(existingEditor.setReadOnly) !== "undefined") {
if (el.prop("disabled")) {
existingEditor.setReadOnly(true);
} else {
existingEditor.setReadOnly(false);
}
}
} catch (ex) {
//do nothing
}
If you have multiple ckeditors on a page. Below solution worked for me.
CKEDITOR.instances.instance_name.on('instanceReady', function(ev) {
var editor = CKEDITOR.instances.instance_name;
//for disable
editor.setReadOnly();
//for enable
editor.setReadOnly(false);
});
精彩评论