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:
JavaScript
x
4
1
{
2
"header_title": "<strong>test</strong>"
3
}
4
JS:
JavaScript
1
9
1
const newTranslations = await fetchTranslationsFor(
2
newLocale,
3
);
4
5
async function fetchTranslationsFor(newLocale) {
6
const response = await fetch('/lang/en.json');
7
return await response.json();
8
}
9
To render it i do like so: pseudo.
JavaScript
1
2
1
element.innerText = json.myprop;
2
Advertisement
Answer
Change innerText
to innerHTML
. When you use the text method, it escapes the html characters. Innerhtml renders the exact html.
JavaScript
1
2
1
element.innerHTML = json.myprop;
2