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 enable back the CKEditor using jQuery please?
Thanks to these changes CKEditor 4 automatically works with the official jQuery Form Plugin for Ajax-based forms. It does not require any action from the developer's side to support it.
visibility = 'hidden';"+ "document. getElementById(\"cke_\"+editor.name). style.
CKEditor 5 offers an out of the box read-only mode. The feature does not require any additional plugin and the editor can be set into a read-only mode using the editor's Editor#enableReadOnlyMode() method.
The easiest way:
To disable CKEditor:
CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(true);
To enable CKEditor:
CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(false);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With