Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escaping the greater than and less than symbols in javascript [duplicate]

Tags:

javascript

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>
like image 978
Mathew Avatar asked Dec 12 '25 04:12

Mathew


1 Answers

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>
like image 150
charlietfl Avatar answered Dec 13 '25 17:12

charlietfl