DylanT 25
DylanT 25

Reputation: 233

Getting NAN in the Web Console (JavaScript)

So I am very new to the world of JavaScript and have been trying to write a simple piece of code. It is to change Celsius into Fahrenheit. (Just the Celsius * 9 / 5 + 32).

I want to enter the Celsius number into the UI and have the answer shown in the DOM (for now - I will change it after so it is shown in the UI after hitting enter.)

But every time I enter the number and click "enter" the DOM shows me NAN.

I know this is probably very basic for most people on this website, but I am new, and any help would be incredibly appreciated.

I have already searched online and found people mentioning parseInt() - but it does not seem to make a difference.

Also, I would like to keep this code as basic as possible - so the way it is now, without the addition of "for loops" and "if else statements".

Here is the code:

var theOne = {
  celcius: function(convert) {
    var c = document.getElementById("celCount");
    var f = c * 9 / 5 + 32;
    console.log(f);
  }
};
<input type="textarea" placeholder="Enter your Celcius here" id="celCount"></textarea>
<button onclick="theOne.celcius()">Enter</button>

Upvotes: 2

Views: 104

Answers (3)

Rupesh S.
Rupesh S.

Reputation: 41

You have used propery document.getElementById(#id).

which provides the dom element not it'is value. Use document.getElementById(#id).value.

Or You can also use jquery for the same. You can get value by $(#id).value

Upvotes: 1

omri_saadon
omri_saadon

Reputation: 10631

use the value property in order to get the value from the DOM element.

As now you get String, you can cast it to Number.

var c = Number(document.getElementById("celCount").value);

Upvotes: 2

beso9595
beso9595

Reputation: 1174

var c = parseInt(document.getElementById("celCount").value);

Upvotes: 1

Related Questions