Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parse html from JSON file

I’m trying to get HTML tags to work in my json-file that i fetch via js.

So i want the return to somehow make the <strong> to work when render it on the page. How would i do that?

Sample of the json:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "header_title": "<strong>test</strong>"
}

JS:

const newTranslations = await fetchTranslationsFor(
    newLocale,
);

async function fetchTranslationsFor(newLocale) {
    const response = await fetch('/lang/en.json');
    return await response.json();
}

To render it i do like so: pseudo.

element.innerText = json.myprop;

>Solution :

Change innerText to innerHTML. When you use the text method, it escapes the html characters. Innerhtml renders the exact html.

element.innerHTML = json.myprop;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading