bootstrap wysihtml5 set Value not working
Asked Answered
A

4

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

Audiology answered 3/5, 2018 at 15:58 Comment(5)
What does the $("#<%=txtDescrizioneBreveCategoria.ClientID%>").wysihtml5(); return you? I think that should return you the editor objectSuction
@TarunLalwani yes, but it adds me 2 toolbars. I have one toolbar foreach time I call wysihtml5().Audiology
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 laterSuction
@TarunLalwani You're right! but how can I set the value to the object? I tryied with val("xxx") but not working..Audiology
Can you create JSfiddle or StackBlitz for me to try the same?Suction
S
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

Suction answered 7/5, 2018 at 10:33 Comment(3)
it tells me Uncaught TypeError: Cannot read property 'setValue' of undefinedAudiology
using editorDescrizioneBreve.data("wysihtml5").editor.setValue(c.DescrizioneBreve, true); it works!!Audiology
@Martina, thanks for the feedback, updated the answerSuction
C
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.

Cobol answered 11/9, 2019 at 8:32 Comment(0)
W
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>
Wove answered 25/5, 2020 at 9:25 Comment(0)
D
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

Donegal answered 4/7, 2023 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.