By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,332 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,332 IT Pros & Developers. It's quick & easy.

How to use JS to get the right codes for HTML characters

P: n/a
Hi,

I have this code

document.getElementById(id).innerHTML = val; // val is the value
someone types in a textbox

Problem is, if someone types "<html>", I would like "<html>" to be
displayed, so the value of the "innerHTML" should really be
"&lt;html&gt;" in this example. Is there a JS function (or does
someone have one) that will do HTML characters replacement?

Thanks, - Dave

Feb 14 '07 #1
Share this Question
Share on Google+
2 Replies

P: n/a
ASM
la***********@zipmail.com a écrit :
>
document.getElementById(id).innerHTML = val; // val is the value
someone types in a textbox

Problem is, if someone types "<html>", I would like "<html>" to be
displayed, so the value of the "innerHTML" should really be
"&lt;html&gt;" in this example. Is there a JS function (or does
someone have one) that will do HTML characters replacement?
val = document?forms[0].foo.value.replace(/</g,'&lt;');

the '&dt;' for '>' is not necessary

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Feb 14 '07 #2

P: n/a
On Feb 14, 10:09 am, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Hi,

I have this code

document.getElementById(id).innerHTML = val; // val is the value
someone types in a textbox

Problem is, if someone types "<html>", I would like "<html>" to be
displayed, so the value of the "innerHTML" should really be
"&lt;html&gt;" in this example. Is there a JS function (or does
someone have one) that will do HTML characters replacement?

Thanks, - Dave
What you probably want, rather than setting innerHTML, is
document.createTextNode:

var el = document.getElementById(id);
el.innerHTML = ''; // clear the node
el.appendChild(document.createTextNode(val));

-David

Feb 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.