Load data into TinyMCE programatically





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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 backrgound. 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 programatically, 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)










share|improve this question





























    0















    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 backrgound. 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 programatically, 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)










    share|improve this question

























      0












      0








      0








      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 backrgound. 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 programatically, 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)










      share|improve this question














      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 backrgound. 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 programatically, 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)







      javascript tinymce textarea richtextarea






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 14:55









      MartyMarty

      6119




      6119
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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.






          share|improve this answer

































            0














            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.






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024729%2fload-data-into-tinymce-programatically%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              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.






              share|improve this answer






























                0














                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.






                share|improve this answer




























                  0












                  0








                  0







                  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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 4 at 5:07

























                  answered Jan 3 at 21:20









                  Michael FrominMichael Fromin

                  6,8822820




                  6,8822820

























                      0














                      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.






                      share|improve this answer




























                        0














                        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.






                        share|improve this answer


























                          0












                          0








                          0







                          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.






                          share|improve this answer













                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 10 at 13:59









                          MartyMarty

                          6119




                          6119






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024729%2fload-data-into-tinymce-programatically%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              Npm cannot find a required file even through it is in the searched directory