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

Discussion in 'Javascript' started by laredotornado@zipmail.com, Feb 14, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. ASM Guest

    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é
     
    ASM, Feb 14, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre Soares

    Converting accent characters to html codes

    Alexandre Soares, Sep 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    690
  2. Greg  --
    Replies:
    4
    Views:
    2,170
  3. Replies:
    2
    Views:
    2,842
    Malcolm
    Aug 20, 2005
  4. Allen
    Replies:
    1
    Views:
    657
    Mark Rae [MVP]
    Dec 3, 2007
  5. Paul
    Replies:
    1
    Views:
    1,075
Loading...

Share This Page