Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert HTML programmatically in wysiHTML5 editor

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?

like image 731
daemon Avatar asked May 24 '12 14:05

daemon


3 Answers

assuming you have instantiated the editor previously using $('#textarea-id').wysihtml5()

$('#textarea-id').data("wysihtml5").editor.setValue('new content');

font

like image 134
Rodrigo R Novaes Avatar answered Nov 15 '22 19:11

Rodrigo R Novaes


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>");
});
like image 27
mateusmaso Avatar answered Nov 15 '22 17:11

mateusmaso


mateusmaso's solution gave me the following error:

NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMHTMLDocument.execCommand]
[Break On This Error]   

Object.defineProperty(object, property, config);

So I've investigated some more and found the following solution, which (IMO) seems more ok:


var value = 'whatever you want to set';
// The jQuery reference to the textarea
var $ta = $('textarea#your-selector');
// The reference to the wysihtml5 editor - everything is contained within it 
var w5ref = $ta.data('wysihtml5');
// Check if you have it
if(w5ref){
   // if yes it's really an enhanced / wysihtml5ed textarea 
   // and use its setter
   w5ref.editor.setValue(value);
} else {
   // otherwise just simply populate the textarea as you normally would
   $ta.html(value);
}

Source

like image 35
Matyas Avatar answered Nov 15 '22 18:11

Matyas