0

I have 2 inputs.

When I put 2 in the n1 input, the second input says 4.

But if I don't put anything in the first (n1), the second inpout (n2) says NaN.

How remove this NaN?

<td><input type="number" value=0 id="n1" oninput="test()"/> <br/><br/></td>

<td></td>

<td><input type="text" value=0 id="result" readonly/><br><br></td>
function test () {
  var n1 = parseFloat(document.getElementById('n1').value);

  var oper = document.getElementById('selector1').value;
  var oper2 = document.getElementById('selector2').value;

  if (oper === 'm' && oper2 === 's') {
    document.getElementById('result').value = n1 * 60;
  }
}
j08691
  • 204,283
  • 31
  • 260
  • 272
Valy
  • 49
  • 6
  • You should post the code for `test()` – Pointy Feb 10 '21 at 17:40
  • Where is the javascript code that does this? – Wais Kamal Feb 10 '21 at 17:40
  • Does this answer your question? [Convert NaN to 0 in javascript](https://stackoverflow.com/questions/7540397/convert-nan-to-0-in-javascript) – Robert Harvey Feb 10 '21 at 17:41
  • function test() { var n1 = parseFloat(document.getElementById('n1').value); var oper = document.getElementById('selector1').value; var oper2 = document.getElementById('selector2').value; if(oper === 'm' && oper2 === 's') { document.getElementById('result').value = n1*60; } } – Valy Feb 10 '21 at 17:42
  • is this please help – Valy Feb 10 '21 at 17:43

2 Answers2

1

Change this line:

var n1 = parseFloat(document.getElementById('n1').value);

to this one:

var n1 = parseFloat(document.getElementById('n1').value) || 0;

This will give n1 a value of 0 if you leave its input field blank.

Wais Kamal
  • 5,858
  • 2
  • 17
  • 36
  • instead of the value 0 is it possible to make it show nothing? – Valy Feb 10 '21 at 17:52
  • Yes. Change `document.getElementById('result').value = n1 * 60;` to `document.getElementById('result').value = (n1 * 60) || "";` – Wais Kamal Feb 10 '21 at 17:55
  • Could you explain to me why it works to just put it there? var n1 = parseFloat(document.getElementById('n1').value) || ""; This because don't work? – Valy Feb 10 '21 at 17:59
  • If you put it after `var n1 = ...` then `n1` would get the value `""`. When you multiply `""` by 60 you get 0. You need to set the input's value itself to `""`. – Wais Kamal Feb 10 '21 at 18:01
0

If you wrap your calculation in an if statement checking to make sure n1 is a number before running, you can make sure that the function does not run if no value is set.

<td> <input type="number" value=0 id="n1" oninput="test()"/> <br/><br/> </td>

<td> </td>

<td> <input type="text" value=0 id="result"  readonly /><br><br> </td>

<script type="text/javascript">
  function test() {
    var n1 = parseFloat(document.getElementById('n1').value);
    var oper = document.getElementById('selector1').value;
    var oper2 = document.getElementById('selector2').value;
    if (oper === 'm' && oper2 === 's') {
      if (isNaN(n1) != true){
        document.getElementById('result').value = n1 * 60;
      }
    }
}
</script>
Jay
  • 1
  • 2