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,715
  • 4
  • 18
  • 26

2 Answers2

3

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

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
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
  • 6,976
  • 2
  • 26
  • 48