-1
<script>
function runCals() {
   var milesRun = document.getElementById("runDist")
   var runCals = Number(milesRun) * 96
   document.getElementById("cals").innerHTML = runCals
}

</script>

I am getting NaN and I do not understand why. I visited many stack questions but none I found had my specific problem.

Why am I getting NaN and how do I fix it?

2
  • 1
    What do you get if you do console.log(Number(milesRun))? Commented Jun 6, 2017 at 1:14
  • My computer has inspect element blocked. Commented Jun 6, 2017 at 1:15

1 Answer 1

3

milesRun is not a number, it's a DOM node, so you can't convert it to a number. If it's an input box just add .value to the end of the selector to extract the value inputted into it. e.g.

function runCals() {
   var milesRun = document.getElementById("runDist").value
   var runCals = Number(milesRun) * 96
   document.getElementById("cals").innerHTML = runCals
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks so much! I will accept this answer once this site enables me to, in 12 minutes.
There’s some wrong terminology here. milesRun is a value. It’s just an object, instead of the intended number.
Lol no worries, we all make silly mistakes sometimes.
@Xufox From Google Dictionary "Value" the numerical amount denoted by an algebraic term; a magnitude, quantity, or number. But you're right in that it might be a bit confusing so I added "number" value. It wasn't wrong terminology.
@joe-tom In JavaScript terms, all of those things are values. There’s a distinction of primitive values, but objects are values, too.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.