How to convert HTML elements to actual readable html?

Viewed 95

I'm trying to query data from database (which is in markdown) and from the server I use Marked.js to parse it to html again. but when I put it in the DOM it displays this:

<h1 id="h1">h1</h1> <h2 id="h2">h2</h2> <h3 id="h3">h3</h3>

I need it to become readable text again, how can I do that?

2 Answers

when rendering the HTML tags to DOM, use .innerHTML

document.getElementById("#id").innerHTML ="html tags goes here".

Im guessing you getting this data as a string? If so you could try the following:

var s = '<h1 id="h1">h1</h1> <h2 id="h2">h2</h2> <h3 id="h3">h3</h3>' //Your string
var child = document.createElement('div'); //Create a wrapper element
child.innerHTML = s; //Put your string as innerHtml
document.getElementById('id_of_your_container').appendChild(child); //append the new element to the dom so it gets visible`
Related