views:

38

answers:

1

Why when I set a form with the numeric character reference of an unicode with javascript the character is not converted to its correct representation, but when I set the form directly from the html it works?

<html>
<head>
    <script language="javascript">
        function test()
        {
            document.InputForm.TextBox.value = '&#1495;';
        }
    </script>
</head>
<body>
  <form name="InputForm" ID="Form1">
    <input id="Text1"  type="text" name="TextBox" style="color: 000000;  position: absolute; left: 0; top: 200; width: 600px; " value="&#1495;">
    </input>
  </form>
  <form>
   <input TYPE="button" Value="Button1" onClick="test();">
  </form>
  <form>
    <input TYPE="button" Value="Button2" onClick="document.InputForm.TextBox.value = '&#1495;'">
  </form>
</body>

+2  A: 

Try changing your Javascript string to '\u1495'

\u is the Unicode escape sequence in Javascript

And the code:

document.InputForm.TextBox.value = '\u1495';
Pekka
@Pekka: As far as I know, there isn't a built-in function; however, see this for a replacement: http://phpjs.org/functions/get_html_translation_table:416
Piskvor
@Pekka: see [Alt attribute encoding with JavaScript](http://stackoverflow.com/questions/2775328/alt-attribute-encoding-with-javascript). The accepted answer only works with numeric representations, my answer works with all other HTML entities as well.
Marcel Korpel
@Piskvor @Marcel interesting, thanks for the links!
Pekka
changing the string to \u1495 didn't work, now it is displaying those square characters indicating that I don't have a font that has this unicode char. So, I have to follow Marcel's answer. http://javascript.internet.com/snippets/convert-html-entities.html
@jamlee: Oh please, don't use *that* thing, but my adjusted code in [my answer](http://stackoverflow.com/questions/2775328/alt-attribute-encoding-with-javascript/2775436#2775436). The one you linked to contains exactly the problems pointed out in Bobince's comments.
Marcel Korpel