Appending text to text area using WYSIHTML5 and jQuery

Viet picture Viet · Nov 2, 2012 · Viewed 8.3k times · Source

I'm using the latest (0.3.0) version of WSYIHTML5 for a message board. One of the features of the board is a quote post option. I've been trying to figure out how to append the quote to the text area.

I have the following jQuery as a test

$('.quote').click(function(){
  $('textarea').append('test this');
  alert($('textarea').text());
});

The alert shows the text has appended to the hidden textarea, but it does not update in the iframe of the WYSIHTML5 view.

What's weird is if I straight up append the text after initializing the WYSIHTML5, it appends just fine.

Any ideas on getting this to work?

Answer

Viet picture Viet · Nov 2, 2012

So in order to update the iframe and have it format any text with HTML, I did the following..

var value = editor.getValue();
var text = '<b>some text</b>;'
editor.setValue(value + text, true);

This is assuming WYSIHTML5 is instantiated under the variable 'editor'. It'll append the quote, plus format the text correctly. The boolean value is if you want the value to be run through the WYSIHTML5 parser rules.

If anyone knows a better way of doing it, I'm all ears.