0

I'm trying to use some entities for my E-learning project. I used to change the input field dynamically from an array

  var equationValue = new Array("",'−'+1,0,2,3);

and Here is my code

<html>
  <head>

  <script type="text/javascript">
      var equationValue = new Array("",'&#8722;'+1,0,2,3);
      function func(){
      document.getElementById('t_1_1').value = equationValue[1];
      }
 </script>
 </head>

 <body onload='func();'>  
   <input type='text' id='t_1_1' />  
 </body>

</html>

But the input value doesn't seem to support HTML entities.

Help me how to use HTML entity in to input value. Here is my working Example

Veeramani3189
  • 23
  • 1
  • 7
  • i searched some tuts. It didn't helped me.. Thanks in Advance :-) – Veeramani3189 Feb 18 '14 at 05:37
  • See if this helps http://stackoverflow.com/questions/5796718/html-entity-decode – elclanrs Feb 18 '14 at 05:38
  • Rather than `new Array("",'−'+1,0,2,3);` it is considere better to use an array literal: `["",'−'+1,0,2,3];` – RobG Feb 18 '14 at 06:07
  • whats the difference Array and Array Literal @RobG – Veeramani3189 Feb 18 '14 at 06:34
  • They are different ways to create an Array. **Array constructor**: `a = new Array(9)` creates an array of length 9. `a = new Array(9, 10)` creates an array of length 2 with members `9,10`. **Array literal** `a = [9]` creates an array of length 1 with member `9`. An array literal `a = [9,10]` creats an array of length 2 with members `9,10`. See the difference? :-) – RobG Feb 18 '14 at 06:54
  • I can't see the difference between these two('a = new Array(9,10)' and 'a = [9,10]').. Is there any difference than storage, i mean the length of the array. @RobG – Veeramani3189 Feb 18 '14 at 07:27
  • They produce an identical result. Literals are preferred given the different behaviour of the constructor based on the number of arguments—and a literal is less to type. Also Douglas Crockford, a JavaScript elder statesman, hates "new" so promotes its disuse. ;-) – RobG Feb 18 '14 at 12:28

3 Answers3

1

you can try this:

var equationValue = new Array("",'&#8722;'+1,0,2,3);
    function func(){
    var mdiv = document.createElement("div");
    mdiv.innerHTML = equationValue[1];
    document.getElementById('t_1_1').value = (mdiv.textContent || mdiv.innerText);
}
Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
  • *innerText* is not supported by all browsers in use, it's an IE method that has been copied by some other browsers. You can use `… = (mdiv.textContent || mdiv.innerText);` or equivalent for cross browser support. – RobG Feb 18 '14 at 06:04
0

What you will need to do is directly paste the entity as text into the input or use a function to convert it to text from the entity value.

var equationValue = new Array("",'−'+1,0,2,3);

So... it would look something like

    <html>
  <head>

  <script type="text/javascript">
  var equationValue = ["",'&#8722;'+1,0,2,3];;

function func(){
    var mdiv = document.createElement("div");
    mdiv.innerHTML = equationValue[1];
    document.getElementById('t_1_1').value = (mdiv.textContent || mdiv.innerText);
}

 </script>
 </head>

 <body onload='func();'>  
   <input type='text' id='t_1_1' />  
 </body>

</html>
Joseph
  • 1,076
  • 10
  • 22
  • The strip script and HTML tags part is completely unnecessary (and error prone if it wasn't). Also, textContent is not supported by IE 8 and older. Also, it is considered much better to use an array literal rather than the Array constuctor since behaviour of the latter is dependent upon the number of arguments supplied. – RobG Feb 18 '14 at 06:02
  • @Veeramani3189 I was mostly updating for anyone else that may try and use the solution since it is cleaner. Credit goes to the others that posted, but I didn't want the accepted answer to be the worst :) – Joseph Feb 18 '14 at 06:29
0

Instead of '&#8722;', use '\u2212'. In JavaScript, &#8722; is as such just seven data characters (though there are ways to make JavaScript interpreters process it as an HTML character reference). The general JavaScript escape notation for a character in a quoted string is \uxxxx where xxxx is four hexadecimal digits that specify the Unicode code number. The number 8722 in hex is 2212.

Alternatively, use '−', containing the character U+2212 MINUS SIGN as such, provided that the file containing your JavaScript code is UTF-8 encoded and declared as such. The way to type MINUS SIGN depends on your editing program and underlying system.

There are other issues with the code, discussed in other answers and comments, but this should answer the question asked.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Why does the number `8722` become `2122`? – evolutionxbox Aug 08 '17 at 10:25
  • The number 8722 (in decimal system) becomes 2122 when represented in hexadecimal (base 16) system. The HTML notation `−` has the number in decimal system; the JavaScript notation `\u2122` needs to have the number in hexadecimal system. – Jukka K. Korpela Aug 09 '17 at 11:23