0

The code I have currently produces a html page with with "ac" on it, what I want is to have literally what the value of the string is (I would type it out here but I'm having the same problem). In words, what I want is "a" then the less than symbol then "b" then the greater than symbol then "c".

Thanks

<!DOCTYPE html>
<html>
<body>
<p id="output"></p>

<script>

document.getElementById("output").innerHTML = "a<b>c"

</script>

</body>
</html>
Mathew
  • 1,104
  • 4
  • 22
  • 46

2 Answers2

5

Set it as text instead of as html

document.getElementById("output").textContent = "a<b>c"
<p id="output"></p>

Or convert to html entities

 var str = "a<b>c".replace('<','&lt;').replace('>','&gt;')
 document.getElementById("output").innerHTML = str;
<div id="output"></div>
charlietfl
  • 169,044
  • 13
  • 113
  • 146
3

Try the following way:

document.getElementById("output").innerHTML = "a<b>c".replace(/</g, '&lt;').replace(/>/g, '&gt;')
<div id="output"</div>
Mamun
  • 62,450
  • 9
  • 45
  • 52