1

I'm using .innerHTML to write strings to an HTML page. I need to have things like &lt; and &gt; to print alongside < and > on the page. However, &lt; consistently gets printed out as <. I've heard that I can do this if I write to the page as HTML rather than text, but I thought .innerHTML was doing that. Is there an escape character I can use or is there something I can do with JS to get this done?

Jackson Egan
  • 2,695
  • 4
  • 16
  • 26

2 Answers2

3

You need to write out &lt; as &amp;lt;.

Michael Irigoyen
  • 21,897
  • 17
  • 84
  • 130
1

You can use <pre>&lt;</pre> and <pre>&rt;</pre> tags in your HTML output .. This should give you the output you are looking for.

Zak
  • 5,910
  • 2
  • 23
  • 46