Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tinymce auto set readonly if the textarea has readonly

Tags:

jquery

tinymce

How would I set tinymce to automatically set the editor as readonly if the textarea already has the attr("readonly")

like image 546
John Magnolia Avatar asked Sep 25 '12 13:09

John Magnolia


People also ask

How do you make TinyMCE readonly?

Use the checkbox to toggle between the "design" and "readonly" modes.

How do I reinitialize TinyMCE?

Use tinymce. remove() method to remove TinyMCE editor from the HTML element and again call tinymce. init() on the selector to reinitialize.

Is TinyMCE responsive?

The TinyMCE editor can be made responsive by using css media queries. Simply add css rules that set the width property of table.

What is TinyMCE selector?

Selector engine, enables you to select controls by using CSS like expressions. We currently only support basic CSS expressions to reduce the size of the core and the ones we support should be enough for most cases.


2 Answers

A more general solution might be

tinyMCE.init({
    ...
    setup: function(ed) {
        if($('#'+ed.id).attr('readonly'))
            ed.settings.readonly = true;
    }
});

This will work for all textareas without you having to specify any ids. In addition it will actually set the editor to readonly mode, and not only prevent editing.

like image 71
Magnar Myrtveit Avatar answered Sep 29 '22 11:09

Magnar Myrtveit


In case you have the following textarea

<textarea id="my_textarea_id" readonly="readonly">Some content here.</textarea>

you may save the knowledge of having a readonly attribute to a variable:

var readonly = $("textarea#my_textarea_id").attr("readonly") ? 1 : 0;

Now the tinymce init function. We choose mode none to init the editor later.

tinyMCE.init({
        theme : "advanced",
        mode: 'none',
        readonly : readonly,
        ...
});

Here, you can init your editor instance using the id of your textarea

tinyMCE.execCommand("mceAddControl", true, "my_textarea_id");

Your editor instance will be initialized as readonly.

like image 28
Thariama Avatar answered Sep 29 '22 12:09

Thariama