Add text to Froala text editor
Asked Answered
A

9

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.

Annual answered 22/7, 2014 at 16:48 Comment(0)
T
16

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

Tenorio answered 30/1, 2016 at 7:8 Comment(3)
Hello, I have done this. On edit, the form, when I submit the form then this field didn't post data. what will do to post this field? Thank youZr
This worked for me. Like... var notification_html = $(this).data('html'); $("textarea#feditor").froalaEditor('html.set', notification_html);Sidekick
What was the final approach?Booker
B
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>');
})
Byssus answered 3/6, 2019 at 23:36 Comment(0)
L
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);
});
Leekgreen answered 25/8, 2014 at 12:41 Comment(0)
U
2

With the latest version of Froala this works fine

$("#froalaEditor")[0]["data-froala.editor"].html.set('This should work fine');
Unleavened answered 3/6, 2020 at 2:0 Comment(0)
Q
1

To append a text use html.insert

$("#editor").froalaEditor('html.insert','<p>new text to append</p>');
Quorum answered 26/6, 2020 at 15:26 Comment(0)
E
0

After inserting the HTML use "Sync"

$("#editor").editable("insertHTML", "123456", true);
$("#editor").editable("sync");
Exploratory answered 28/7, 2014 at 10:1 Comment(0)
S
0

Just for note. in HTML

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

and in javascript

  $(function() {
    $('div#froala-editor').froalaEditor({
      pastePlain: true
    })
  });
Studbook answered 4/11, 2018 at 16:22 Comment(0)
B
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

Booker answered 29/8, 2019 at 4:43 Comment(0)
B
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);
            },
Bandit answered 16/5, 2022 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.