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

L

laredotornado

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
 
A

ASM

(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top