'make readonly/disable tinymce textarea
I need to disable or make readonly a tinymce textarea at runtime.
Solution 1:[1]
From version 4.3.x on you can use code below for readonly mode
tinymce.activeEditor.setMode('readonly');
and for design mode:
tinymce.activeEditor.setMode('design');
Solution 2:[2]
IF you only have one editor, this works:
tinymce.activeEditor.getBody().setAttribute('contenteditable', false);
If you have multiple editors, you have to select them by the id of the textarea:
tinyMCE.get('textarea_id').getBody().setAttribute('contenteditable', false);
Solution 3:[3]
Thariama's solution will set all TinyMCE textareas on the page to readonly.
The best solution I've found was posted by Magnar Myrtveit which will set fields to readonly that have the readonly attribute. Here is the code:
tinyMCE.init({
...
setup: function(ed) {
if ($('#'+ed.id).prop('readonly')) {
ed.settings.readonly = true;
}
}
});
Solution 4:[4]
To disable you can call this command:
tinymce.EditorManager.execCommand('mceToggleEditor', true, tinymceId);
And to again enable the editor you can again call this command.
'mceToggleEditor' command toggles the WYSIWYG mode on or off by displaying or hiding the textarea and editor instance. This is not the same thing as mceAddControl or mceRemoveControl because the instance is still there and uninitialized, so this method is faster.
Link for above command: http://archive.tinymce.com/wiki.php/TinyMCE3x:Command_identifiers
Solution 5:[5]
For the latest 5.x version, use
editor.mode.set('readonly')
and
editor.mode.set('design')
Solution 6:[6]
The best way I found (this is via tinymce-react, but should work with js as well), is to set the control to disabled. Using tinymce 5.2.
<Editor
initialValue={details}
disabled = {true}
init = {{
height: 300,
menubar: false,
toolbar: false,
readonly: true
}}/>
Solution 7:[7]
You can see this answer here by @rioted: https://stackoverflow.com/a/34764607/1827960.
I used it to come up with this solution:
tinymce.settings = $.extend(tinymce.settings, { readonly: 1 });
tinymce.EditorManager.editors.forEach(function (editor) {
tinymce.EditorManager.execCommand('mceRemoveEditor', false, editor.id);
//tinymce.EditorManager.editors = [];
tinymce.EditorManager.execCommand('mceAddEditor', false, editor.id);
});
Solution 8:[8]
you can use
this.getBody().setAttribute('contenteditable', false);
take look at full solution,, my server side is Asp.net MVC
setup: function (ed) {
ed.on('init', function () {
this.execCommand("fontSize", false, "17px");
$("html,body").scrollTop(0);
@if (ViewBag.desableEdit != null && ViewBag.desableEdit == true)
{
<text>
this.getBody().setAttribute('contenteditable', false);
</text>
}
});
anather way to do it if you have server side condition which will be removed in the returned HTML
tinymce.init({
selector: ... ,
....
@if (ViewBag.desableEditExseptExportNumber != null && ViewBag.desableEditExseptExportNumber == true)
{
<text>
readonly: 1,
</text>
}
language: 'ar',
....});
Solution 9:[9]
getInit(): any
{
const result = {
base_url: baseUrl,
menubar: false,
branding: false,
height: '500',
selector: 'textarea', // change this value according to your HTML
toolbar: false,
statusbar: false,
readonly: true,
setup: function(ed)
{
ed.settings.readonly = true;
}
};
return result;
}
Solution 10:[10]
Maybe this code line helps in others browsers using iframes.
tinymce.activeEditor.getBody().contenteditable = false
Regards!
Solution 11:[11]
That works for ASP.NET MVC Razor
readonly: @(Model.Readonly ? "true" : "false")
while initializing tinyMCE:
tinymce.init({/* put readonly setting here */});
Solution 12:[12]
disabled={true} React integrations
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
