Javascript to update a rich text field

Thought I'd share - I had a need to insert formatted data into a rich text field via Javascript, and thanks to Stakoverflow the key method is:

$('#field_XXX').redactor('code.set', htmltext);

Of course build the variable htmltext with html for it to work.