10

I'm trying to programatically input html into Froala text editor. Based on their documentation this should work:

$(function(){
    $('#editor').editable("setHTML", "<p>My custom paragraph.</p>", false);
});

But when I reload page I only get empty space, no errors. This is binded to textarea with id #editor. If I use this code:

$(function(){
    $('#editor').editable({inlineMode : false});
});

... then everything is ok.

Does anyone knows how to programatically input html into this editor.

Alen
  • 1,750
  • 7
  • 31
  • 62

9 Answers9

15

You should try this :

$('#editor').froalaEditor('html.set', 'My custom paragraph.');

See the detail about froala method at this reference: https://www.froala.com/wysiwyg-editor/docs/methods#html.set

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
5

For the users you are looking for v3 answers, You can add text in v3 by:

var editor = new FroalaEditor('.selector', {}, function () {
  // Call the method inside the initialized event.
  editor.html.insert('<p>My custom paragraph.</p>');
})

Also for if you want to replace the whole content then you can use set method

var editor = new FroalaEditor('.selector', {}, function () {
  // Call the method inside the initialized event.
  editor.html.set('<p>My custom paragraph.</p>');
})
Ilyas karim
  • 4,592
  • 4
  • 33
  • 47
2

Use both. First you have to create the editor, then you can set the HTML

$(function(){
    $('#editor').editable({inlineMode : false});
    $('#editor').editable("setHTML", "<p>My custom paragraph.</p>", false);
});
Hans Roerdinkholder
  • 3,000
  • 1
  • 20
  • 30
2

With the latest version of Froala this works fine

$("#froalaEditor")[0]["data-froala.editor"].html.set('This should work fine');
Gvs Akhil
  • 2,165
  • 2
  • 16
  • 33
1

To append a text use html.insert

$("#editor").froalaEditor('html.insert','<p>new text to append</p>');
Savrige
  • 3,352
  • 3
  • 32
  • 38
0

After inserting the HTML use "Sync"

$("#editor").editable("insertHTML", "123456", true);
$("#editor").editable("sync");
RailsEnthusiast
  • 1,251
  • 2
  • 12
  • 18
0

Just for note. in HTML

<div id="froala-editor">
  <p>something</p>
</div>

and in javascript

  $(function() {
    $('div#froala-editor').froalaEditor({
      pastePlain: true
    })
  });
0

Try this:

$('#editor').froalaEditor('html.set', 'My custom paragraph.');
$('#editor').froalaEditor('undo.saveStep', 'My custom paragraph.');

Reference: https://github.com/froala/wysiwyg-editor/issues/1578

Naik Ashwini
  • 750
  • 12
  • 32
0

If you are using it with React js, here is how i did it... FroalaEditorComponent has an event object in it which has initialized as an event. here is the code example:

initialized: function () {
               this.html.set(formik?.values?.body); // not working for me
               this.html.set(`<h1>Hello!</h1>`); // working fine
               console.log(this);
            },
Asad Ashraf
  • 1,425
  • 8
  • 19