Paste HTML programmatically in wysiHTML5 editor

I found the javascript editor wysiwyg wysiHTML5 .

I am trying to add the <a href=...> element to the editor, or just programmatically include bold.

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?

+9
javascript wysiwyg wysihtml5
source share
3 answers

Not really, but you have to be sure that your textarea (iframe) is focused. Try using on instead of observe . Here is an example of the code that worked for me with insertHTML.

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

mateusmaso 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 researched a few more and found the following solution, which (IMO) seems more convenient:

 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 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); } 

A source

+8
source share

assuming you created an editor instance using $('#textarea-id').wysihtml5()

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

font

+1
source share

All Articles