I was wondering if there is a way I could apply htmlentities()
to var x
so that if I put <p>hello</p> into the text area
and press the button it would display <p>hello</p> instead of hello using the below code.
Thanks in advance
function myFunction1() { var x = document.getElementById("myText1").value; document.getElementById("demo1").innerHTML = x; }
<textarea name="myText" id="myText1" type="text"></textarea> <button onclick="myFunction1()">thing</button> <p><span id="demo1"></span></p>
You need to use <xmp>
tag
<xmp> <p>hello</p> </xmp>
function myFunction1() { var x = document.getElementById("myText1").value; document.getElementById("demo1").innerHTML = x; }
<textarea name="myText" id="myText1" type="text"></textarea> <button onclick="myFunction1()">Show</button> <p><xmp id="demo1"></xmp></p>
Generally this is done by replacing the <
symbol with the "less than" HTML encoding ( <
), and >
with "greater than" ( >
).
For instance, you should use basic string methods to replace something like
"<p>hello</p>"
with
"<p>hello</p>"
(Otherwise, adding it to an element's innerHTML will read it as HTML, including the tags.)
I should point out that you can also try just wrapping everything in the <pre>
tag, but I've had varying results with it. I think the method above is a safer bet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.