0

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:

{
    "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;
6
  • How have you attempted to render it on the page? Commented Apr 26, 2022 at 17:22
  • 3
    If you do something.innerHTML = newTranslations.header_title you should see it rendered (something is a variable containing the element you want to display it in). Commented Apr 26, 2022 at 17:23
  • What's the question here? You have a working solution based on the code you've provided Commented Apr 26, 2022 at 17:26
  • How i make the html tags in the json to display as html and not plain strings. Commented Apr 26, 2022 at 17:27
  • Where/when is your rendering code called? What does your html look like? What is element? What is json? Be sure to read How to create a minimal reproducible example. Commented Apr 26, 2022 at 17:28

1 Answer 1

1

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

element.innerHTML = json.myprop;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.