Load data into TinyMCE programmatically

910 views Asked by At

I have multiple text-areas that I'd like to enhance using tinyMCE. I can get the text areas to show as the Rich Text Editors by initializing TinyMCE on all text areas in the page as below:

$(function () {
    tinymce.init({
        selector: "textarea",
        statusbar: false,
        setup: function (editor) {
            editor.on('change', function () {
                editor.save();
            });
        }
    });
});

This also handles the sync process between the Tiny editor and the actual textarea.

My html, that populates the text areas looks like this:

<div id="divEditor1" class="container-fieldset">
    <div class="editor-label-field" style="left: 50px">
        <%:Html.LabelFor(Function(model) model.divEditor1, "divEditor1")%>
    </div>
    <div class="editor-field-fn">
        <%:Html.TextBoxFor(Function(model) model.divEditor1,  New With { Key .class = "ucase-input" } )%>
        <%:Html.ValidationMessageFor(Function(model) model.divEditor1)%>
    </div>
</div>
    <div id="divEditor2" class="container-fieldset">
    <div class="editor-label-field" style="left: 50px">
        <%:Html.LabelFor(Function(model) model.divEditor2, "divEditor2")%>
    </div>
    <div class="editor-field-fn">
        <%:Html.TextBoxFor(Function(model) model.divEditor2,  New With { Key .class = "ucase-input" } )%>
        <%:Html.ValidationMessageFor(Function(model) model.divEditor2)%>
    </div>
</div>
... etc

I can read the content from the TinyMCE editors like this:

for (var i = 0; i < numberOfEditors; i++) {           
    sFieldValue = document.getElementById("FormFieldText" + i).value;
    //sFieldValue = tinyMCE.get("FormFieldText" + i).getContent(); -> or like this, works just as well.
};

The problem I am having is getting the TinyMCE editor box to display the already existing text on page load (text read from a database), since it always shows up as an empty text box. However, the text is imported correctly in the original textarea in the background. Formatted and escaped, since it goes through some ajax calls.

I've seen I can set the content of tiny like this:

tinyMCE.get('my_editor').setContent(data);

However, I need to do it programmatically, in a way that all textareas on my page export the information to tiny. Just like the above

setup: function (editor) {
    editor.on('change', function () {
        editor.save();
    });
    }

Also, what would be the right time to un-encode the text so tiny can read it? (I assume this step is necessary)

3

There are 3 answers

0
Michael Fromin On

If you initialize TinyMCE on each textarea before each textarea has its content then TinyMCE won't auto-magically go back and update itself when the textarea gets updated with content - it just does not work that way.

You could use TinyMCE APIs (as you already know from your post) to update the editor's content once you get the data. Alternatively you could delay initializing TinyMCE until after the data is fetched into each textarea.

Neither approach is better/worse - there are certainly multiple ways to solve this and they all work if implemented appropriately.

0
Marty On

I ended up parsing the list of active tinymce instances by ID and populating each one with the corresponding text from the textareas in the background.

0
Spinstaz On

I came across this post looking for a solution to get the data from the database back into tinymce, and I ended up with

<textarea id="textarea"><?= htmlspecialchars($description); ?></textarea>

Just using php and the variable from the array from the database.