scarba05
scarba05

Reputation: 3001

Can you toggle readonly on a TinyMCE 4.x editor

I'd like to switch a form between enabled / disabled states. The form includes a formatted text field implemented using a tinyMCE 4.0 editor. I can set this to be readonly on load as follows

tinyMCE.on('AddEditor', function(addEditorEvent) {
    addEditorEvent.editor.settings.readonly = true;
});

but subsequently calling

tinyMCE.get('my-editor').settings.readonly = false;

has no effect. Is there a way to trigger the editor to re-initialize or some other way of getting it to pick up this change in state?

Upvotes: 2

Views: 3402

Answers (2)

thorn0
thorn0

Reputation: 10397

Use the setMode method:

editor.setMode('readonly');

Upvotes: 4

Chankey Pathak
Chankey Pathak

Reputation: 21666

<script type="text/javascript">
  tinymce.init({ selector: 'textarea' });
  var is_disabled = false;
  function enable_disable(btn) {
      is_disabled = !is_disabled;
      tinymce.activeEditor.getBody().setAttribute('contenteditable', !is_disabled);
      btn.value = is_disabled ? "Enable" : "Disable";
  }
</script>
<textarea>Your content here.</textarea>
<input type="button" value="Disable" onclick = "enable_disable(this)" />

Upvotes: 0

Related Questions