How to clear TinyMCE draft after submitting?

738 Views Asked by At

I'm using TinyMCE with great results, the only problem now is that using the autosave plugin and after submitting a form, when I try to create a new record the draft for the last submitted form gets loaded. I've spent some hours looking for a solution without success so, if anyone knows how can I clear the draft after submitting (or onSubmit, or something) I'd be very grateful.

Thanks!

2

There are 2 best solutions below

0
On

Would the following help?

<script>
$('#myForm').submit(function(e) {
  e.preventDefault();

  // clear tinymce contents
  tinyMCE.activeEditor.setContent('');
});
</script>
0
On

After the form has been submitted and you can clean up, e.g. empty the content are, also clear the localStorage by key:

// https://www.tiny.cloud/docs/tinymce/6/autosave/#autosave_prefix
localStorage.removeItem(
  `tinymce-autosave-${window.location.pathname}${window.location.hash}${window.location.search}-comment-editor-draft`
);