0

So Currently I have this as my object for Props

{
    id: "1",
    summary: "HTML content inside this string",
    name: "John"
}

so what I was trying to do was remove the string so it would print the html as html not as a string but the error I would get with JSON.parse() is this SyntaxError: Unexpected token o in JSON at position 1

HTML content is a very simple Ul with a list of Li's.

How else can I render an objects value that holds HTML so it prints as HTML.

Any help would be amazing.

Thanks

2
  • The SynataxError you are getting more than likely means you are passing an object to JSON.parse() instead of JSON text, check the variable that you are using (console.log) Commented Aug 29, 2017 at 15:11
  • Removing the quote makes the string invalid. Instead use the string/text to create the required html element, refer stackoverflow.com/questions/2522422/… Commented Aug 29, 2017 at 15:12

2 Answers 2

0

Removing the quote makes the string invalid. Instead use the string/text to create the required html element.

Check below example.

var props = {
  id: "1",
  summary: "<div><ul><li>Some text here</li></ul></div>",
  name: "John"
}

var htmlObject = document.createElement('div');
htmlObject.innerHTML = props.summary;
document.getElementById("t").appendChild(htmlObject);
<div id="t">
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Figured it out and Thank you for all your help.

What I did to get it to render with react was create a function for the output to be modified

summary() {
    return {__html: modal.summary
}

Then I used the dangerouslySetInnerHTML and it worked

<div id="summary" dangerouslySetInnerHTML={summary()} />

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.