0

I make use of an API that consists of a JSON with prefetched data from a CMS. The partial JSON looks like this:

"content": "<p><em>We're looking for a pro-active, analytical, commercially minded and ambitious deal-closer who loves to work - and play - hard to join our Company.

I then pass this data to a child component and then render it by using v-html. I expected this to output the HTML tags with styling and semantics. However, it renders the HTML tags as plain text:

<p><em/>We're looking for a pro-active, analytical, commercially minded and ambitious deal-closer who loves to work - and play - hard to join our Company.

Does anyone know what I am doing wrong? Should I have parsed the JSON? Should I have decoded the raw JSON to HTML tags first?

1 Answer 1

1

Nothing to do with JSON; everything to do with your web service unhelpfully giving you unparsed HTML.

You're going to have to decode these HTML entities yourself.

One common trick is to feed the unparsed HTML to an off-DOM element, then read it back via textContent, which will give you the parsed version.

let p = document.createElement('p');
p.innerHTML ='&lt;p&gt;'
console.log(p.textContent); //"<p>"
Sign up to request clarification or add additional context in comments.

2 Comments

It works, hoorray! Thank you so much; I had been stuck on this issue for a few days. Just out of curiousity: any idea why your trick seems to work?
Well it comes down to the difference between an element's HTML content and its textual content. Textual means parsed - the result of the HTML. Hence it works.

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.