Insert HTML programmatically in wysiHTML5 editor

daemon picture daemon · May 24, 2012 · Viewed 14.9k times · Source

I found javascript wysiwyg editor wysiHTML5.

I'm trying to add element <a href=...> to the editor or just turn on bold programmatically.

My code is:

var editor = new wysihtml5.Editor("textarea", {
    toolbar:      "toolbar",
    stylesheets:  "css/wysihtml5.css",
    parserRules:  wysihtml5ParserRules
});

editor.observe("load", function() {
    editor.composer.commands.exec("bold");
});

Am I doing something wrong?

Answer

mateusmaso picture mateusmaso · May 27, 2012

Actually no, but you have to be sure that your textarea (iframe) is focused. Try to use on instead of observe. Here is a sample code that worked for me with insertHTML.

editor.on("load", function() {
  editor.focus();
  editor.composer.commands.exec("insertHTML","<a href=....>text</a>");
});