6

I have a wysihtml box and I want to fill its value after an ajax call

$("#<%=txtDescrizioneBreveCategoria.ClientID%>").wysihtml5();

function ModificaCategoria(id) {
    $.ajax({
                url: "Categorie.aspx/GetCategoria",
                type: 'POST',
                dataType: "json",
                data: JSON.stringify({ 'id': id }),
                contentType: 'application/json; charset=utf-8',
                cache: false,
                async: false,
                processdata: true,
                traditional: true,
                success: function (data) {
                    var c = data.d;
                        //we need to parse it to JSON 
                        c = $.parseJSON(c);
                        $('#<%=txtTitleCategoria.ClientID%>').val(c.Title);
                        $('#<%=txtDescrizioneBreveCategoria.ClientID%>').val(c.Descrizione);
                }
        });
}

I already tried with

$('#<%=txtDescrizioneBreveCategoria.ClientID%>').contents().find('body').html('<b>New text</a>');

and with

$('#<%=txtDescrizioneBreveCategoria.ClientID%>').html(c.Descrizione);

and with

var editorObj = $("#<%=txtDescrizioneBreveCategoria.ClientID%>").data('wysihtml5');
var editor = editorObj.editor;
editor.setValue(c.DescrizioneBreve);

but editor variable is always undefined I'm using wysihtml5x v0.4.15 link here

Martina
  • 1,852
  • 8
  • 41
  • 78
  • What does the `$("#<%=txtDescrizioneBreveCategoria.ClientID%>").wysihtml5();` return you? I think that should return you the editor object – Tarun Lalwani May 06 '18 at 20:08
  • @TarunLalwani yes, but it adds me 2 toolbars. I have one toolbar foreach time I call wysihtml5(). – Martina May 07 '18 at 08:58
  • You should call it once only and then store the return value and use it. These methods are initialization methods and then you can store the output value in some variable and use it later – Tarun Lalwani May 07 '18 at 09:02
  • @TarunLalwani You're right! but how can I set the value to the object? I tryied with val("xxx") but not working.. – Martina May 07 '18 at 09:12
  • Can you create JSfiddle or StackBlitz for me to try the same? – Tarun Lalwani May 07 '18 at 09:15

4 Answers4

4

You should be able to achieve the same using below

$("#<%=txtDescrizioneBreveCategoria.ClientID%>").wysihtml5();
window.describeEditor = window.editor;

And then later you should can use

describeEditor.setValue(c.DescrizioneBreve, true)

or use

editorDescrizioneBreve.data("wysihtml5").editor.setValue(c.DescrizioneBreve, true);

Where editorDescrizioneBreve is the object returned by $("#<%=txtDescrizioneBreveCategoria.ClientID%>").wysihtml5()

PS: Solution based on https://github.com/jhollingworth/bootstrap-wysihtml5/issues/52

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
3

For me, this worked:

$('.wysihtml5-sandbox').contents().find('body').html(descr)

I have only one Wysihtml5 on my page, with multiple, you need to use more precise selector.

DaWe
  • 1,422
  • 16
  • 26
0

Below code works for me

<textarea class="wysihtml5 form-control" rows="15"  id="txtContent" runat="server"></textarea>

   <script type="text/javascript"> 
      function GetContent() {
      var evnt = {RECORD_ID:'101'};
      $.ajax({
               type: "post",
               url: '/API/GetContent',
               contentType: "application/json; charset=utf-8",
               data: JSON.stringify(evnt),
               success: function (result) {
                  if (result) {             
                      var editorObj = $("#<%=txtContent.ClientID%>").data('wysihtml5');
                      var editor = editorObj.editor;
                      editor.setValue(result.CONTENT);
                  }
              }
          });
           }
    </script>
0

For my work that:

$('#ID OF ELEMENT').data("wysihtml5").editor.setValue('TEXT TO INSERT');

And i have some in the same page. :D

For more information: https://github.com/jhollingworth/bootstrap-wysihtml5/issues/52