1

Write a script that inputs an integer code for a character and displays the corresponding character.

It should print in a paragraph.

function character() {
    var input = document.getElementById( "input" );
    var code = document.getElementById( "output" ).innerHTML = input;
    output.value = String.fromCharCode( code );
}
<input id="input" type="text" size="10">
<br>
<input type="button" value="click here" onclick="character()" id="button">
<br>
<p id="output" ></p>
MarthyM
  • 1,839
  • 2
  • 21
  • 23
Mark Mark
  • 13
  • 4

3 Answers3

1

A P is not a form element. Also if you use form.fieldName, you need to use name="" instead of id=""; otherwise use document.getElementById for all fields and ignore the form.

You need to use innerHTML or innerText/textContent for a paragraph

document.getElementById("output").innerHTML=...

function character() {
  var form = document.getElementById("form");
  var code = parseInt(form.input.value,10); // radix 10 to make decimal
  document.getElementById("output").innerHTML = String.fromCharCode(code);
}
<form id="form">
  <input name="input" type="text" size="10">
  <br>
  <input type="button" value="click here" onclick="character()" id="button">
  <br>
  <p id="output" ></p>
</form>
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • i must make it in p@mplungjan – Mark Mark May 31 '15 at 14:30
  • See update with additional fixes - I may have given you too much information for a homework assignment – mplungjan May 31 '15 at 14:36
  • That is the [radix](http://en.wikipedia.org/wiki/Radix). It is needed to handle people inputting something beginning with 0x where x is < 9 since that is considered [octal](http://en.wikipedia.org/wiki/Octal) – mplungjan May 31 '15 at 14:41
  • it gives me this Vote Up requires 15 reputation @mplungjan – Mark Mark May 31 '15 at 15:12
  • Yes. My favourite. I read the first and second edition to pieces [JavaScript, the Definitive Guide](http://amzn.to/1SLwAkp) - now in 6th edition. – mplungjan May 31 '15 at 15:30
1

You have the problem for access to the elements inside a form.

document.getElementById("form").elements['input']

But this only works with form elements, not with other HTML elements.

In this of elements, you can use id or name but for historical reasons.

You have another way here:

function character() {
  var input = document.getElementById("input"),
      output = document.getElementById("output");
      output.innerHTML = String.fromCharCode(parseInt(input.value), 10) || '';
}
<form id="form">
  <input id="input" type="text" size="10">
  <br>
  <input type="button" value="click here" onclick="character()" id="button">
  <br>
  <p id="output" ></p>
</form>
Raúl Martín
  • 4,471
  • 3
  • 23
  • 42
1

Here, this works:

<script>
function character() {
var ChrCode = document.getElementById("input").value;
document.getElementById("output").innerText = ChrCode + " = " + String.fromCharCode(ChrCode);
//output.value = String.fromCharCode( code );
}
</script>

<input id="input" type="text" size="10">
<br>
<input type="button" value="click here" onclick="character()" id="button">
<br>
<p id="output" >Output</p>
Lance
  • 3,824
  • 4
  • 21
  • 29