Search code examples
javascriptjqueryhtmlwysiwyg

How to render html on the fly?


Client will fetch json formatted data from server:

{"html": "<hr>a<br>"}

And I want to render the html code into a div. How can I do that?

And in fact, I want to display the rendered html page inside a wysiwyg editor.

Is there any Opensource wysiwyg(js, html5, or sth) can do this?


Solution

  • The answer to your first question: after you parse your JSON, you can use insertAdjacentHTML.

    var obj = JSON.parse(json);
    var div = document.getElementById('example');
    div.insertAdjacentHTML('beforeend', obj.html);