N0tACyb0rg
N0tACyb0rg

Reputation: 47

Javascript - Numbers showing as NaN and object undefined

I am trying to make a comment generator for a program we use, and I am having an issue with numbers. I originally got object undefined after doing the math when I used the block of code below:

totalPages = toString(totalPages);
pagesLeft = toString(pagesLeft);
pph = toString(pph);
hour = toString(hour);
minute = toString(minute);
weekTG = toString(weekTG);

Then, I tried using the Number function when I got the values from the HTML inputs:

var totalPages = document.getElementById("totalPages");
var startPage = document.getElementById("startPage");
var endPage = document.getElementById("endPage");
totalPages = Number(totalPages);
startPage = Number(startPage);
endPage = Number(endPage);

When I used the above method, I got NaN. I also tried using parseFloat but that did not work out. Could I have some help? If you would like to look at my code further, you can look here in script.js. The output is currently in the console, just to clear up any future confusion.

Upvotes: 0

Views: 259

Answers (1)

Mamun
Mamun

Reputation: 68933

totalPages, startPage and endPage are the elements themselves, you have to take the value (if element is input) or textContent (if element is other than input) from them:

var totalPages = document.getElementById("totalPages").value;
var startPage = document.getElementById("startPage").value;
var endPage = document.getElementById("endPage").value;

Upvotes: 1

Related Questions