Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 enable back the CKEditor using jQuery please?

like image 494
Bakhtiyor Avatar asked Jun 23 '11 15:06

Bakhtiyor


People also ask

Does CKEditor use jQuery?

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.

How do you hide CKEditor?

visibility = 'hidden';"+ "document. getElementById(\"cke_\"+editor.name). style.

How do I make CKEditor read only?

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.


2 Answers

The easiest way:

To disable CKEditor:

CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(true);

To enable CKEditor:

CKEDITOR.instances['TEXTAREA_NAME'].setReadOnly(false);
like image 151
Dias Avatar answered Oct 05 '22 17:10

Dias


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

like image 25
LoneWOLFs Avatar answered Oct 05 '22 17:10

LoneWOLFs