4

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>

1

1 Answer 1

5

Try changing your Javascript string to '\u1495'

\u is the Unicode escape sequence in Javascript

And the code:

document.InputForm.TextBox.value = '\u1495';
Sign up to request clarification or add additional context in comments.

5 Comments

+1 yup, that's the only alternative that comes to my mind as well. Although it would be pretty interesting to know the way to insert & entities programmatically.
@Pekka: As far as I know, there isn't a built-in function; however, see this for a replacement: phpjs.org/functions/get_html_translation_table:416
@Pekka: see Alt attribute encoding with JavaScript. The accepted answer only works with numeric representations, my answer works with all other HTML entities as well.
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. javascript.internet.com/snippets/convert-html-entities.html
@jamlee: Oh please, don't use that thing, but my adjusted code in my answer. The one you linked to contains exactly the problems pointed out in Bobince's comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.