0

I have a JSON file containing data that I want to display. The work is being done completely in HTML, which picks certain fields to display and writes them to the browser. I have no control over the fields as they are collected automatically from an external source and we want to keep them as original as possible.

I've encountered an issue where some fields are written in HTML, so the string might say something like:

"<p>Foo <strong><Bar></strong</p>"

Is there a way to have HTML when it encounters such a string, display it not as raw characters, but as the correct HTML output? I know which fields will and won't have tags like this, so I don't need to test for the tags. I only need a way to display the tags correctly for a variable string.

wbest
  • 611
  • 1
  • 6
  • 15

1 Answers1

1

You can use some JavaScript:

var example = '"<p>Foo <strong><Bar></strong</p>"'
example = example.replace('"', '')
# Now example => <p>Foo <strong><Bar></strong</p>

Run this inside script tags in your HTML document on strings you'd like to display that have quotes around them.

Edited.

Thomas Hobohm
  • 645
  • 4
  • 9
  • I'm having trouble getting this to then be displayed on the page. Using `document.getElementByID("afct-content-data").innerHTML = example` in the script isn't working. – wbest Aug 01 '14 at 18:19
  • What do you mean by "isn't working". What's happening? Are you getting an error message? Are you sure your javascript is being executed? (Test this by putting `alert("test");` in the script). It's working for me. – Thomas Hobohm Aug 01 '14 at 18:21
  • Okay, yeah. It looks like javascript isn't being executed. There are no errors being output. But also nothing is being output. Nothing happens with the added alert. I'll have to look in to this then. I've never really worked with javascript before and got sort of triaged on to this project yesterday. – wbest Aug 01 '14 at 18:52