1

i am trying to set HTML data in tinymce using jquery. But i have some problem in it.

my html data is as :

  var data = "<div><img src='http://localhost/images/test.png' /><div>Image Title</div></div> html";
    tinyMCE.activeEditor.setContent(data , {format : 'raw'} ); 

It renders the div and image properly. but i get additional

tag with html in it If i remove the html word the data is not properly rendered in editor

I also tried various below combination, but it did not helped:

tinyMCE.activeEditor.dom.setHTML(tinyMCE.activeEditor.id, str);
tinyMCE.activeEditor.setContent(data ); 
user367134
  • 922
  • 8
  • 19
  • 31

1 Answers1

2

Tinymce and the browser taking care of the validity of the html entered.Can you show me the editor content after you entered your data?

tinyMCE.activeEditor.setContent(content);

is the usual correct way to replace the current editor content with the text that the content variable hold.

Thariama
  • 50,002
  • 13
  • 138
  • 166
  • Is there no way to force tinyMCE to set html? Because I use it in a modal dialog and have several textareas on my site. I save the textcontent of the focused textarea in a variable and want to add it to the editor as html. After confirming the html-code shall be saved in the focused textarea. If I would use set content instead it would mean chaos... :( – user2718671 Apr 28 '14 at 08:44
  • 2
    yes, this is possible: tinyMCE.activeEditor.setContent(content, {format : 'raw'}); – Thariama Apr 28 '14 at 08:48
  • 1
    thx for the fast response! Now I use tinyMCE.activeEditor.setContent(text, {format : 'raw'}); to add the focused textareas html-content to the modal editor and j('#' + id).html(tinyMCE.activeEditor.getContent({format : 'raw'})); to overwrite the focused textarea's content with the modified text. I also disabled the auto-p-tagging with force_br_newlines : true, force_p_newlines : false, forced_root_block : '' but there seems to be an encoding error because the wysiwyg displays:
    • Verdi 2
    • fgjgdhkgj
    and the ...
    – user2718671 Apr 28 '14 at 09:01
  • ... html view just: <ul><li>Verdi 2</li><li>fgjgdhkgj<br data-mce-bogus="1"></li></ul> – user2718671 Apr 28 '14 at 09:02
  • 1
    another way to securely overcome this abstable is to reset the editor body by hand: $(editor.getBody()).html(content) – Thariama Apr 28 '14 at 09:07
  • thx again. but with j(tinymce.activeEditor.getBody()).html(text); [j because of noconflict()] and j('#' + id).html(j(tinymce.activeEditor.getBody()).html()); it's still the same. That's strange. Where does that encoding come from? – user2718671 Apr 28 '14 at 09:23
  • Think I found something, but not tested yet: http://stackoverflow.com/questions/1219860/html-encoding-in-javascript-jquery With these encoding/decoding functions it could work ;) – user2718671 Apr 28 '14 at 09:30
  • indeed, j(tinymce.activeEditor.getBody()).html(htmlDecode(text)); did the trick :) Thx for your help again!!! – user2718671 Apr 28 '14 at 09:34
  • glad to have been able to help – Thariama Apr 29 '14 at 08:29