Error NaN when trying to add two input fields in Javascript

I am trying to sum two text fields using Javascript and my result keeps coming back as NaN (not a number?) and I am entering only numbers in the input fields. Despite some debugging in Visual Studio, I still can't figure out why?

Here is my HTML code:

<div id="calc">
        <p>
            <label for="num1">Enter Your First Number:</label>
            <input type="text" name="txtNum1" id="txtNum1" size="5" />
        </p>

        <p>
            <label for="num1">Enter Your Second Number:</label>
            <input type="text" name="txtNum2" id="txtNum2" size="5" />
        </p>

        <input type="button" name="add" id="add" value="Add" onclick="addNum();" />
       

    </div>

        <div id="ShowResults"></div>

Open in new window


And here is my javascript:
var firstNumber = parseInt(document.getElementById('txtNum1').value);
var secondNumber = parseInt(document.getElementById('txtNum2').value);

function addNum() {

        var result = firstNumber + secondNumber;

            resultMessage = "Your answer is: " + result;
            ShowResults.innerHTML = resultMessage;

}

Open in new window


Any help would be appreciated!
kenjpeteAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

put the initialization part into the function you have defined. And it should work.

function addNum() {

var firstNumber = parseInt(document.getElementById('txtNum1').value);
var secondNumber = parseInt(document.getElementById('txtNum2').value);

        var result = firstNumber + secondNumber;

            resultMessage = "Your answer is: " + result;
            ShowResults.innerHTML = resultMessage;

}

Open in new window

0
Rainer JeschorCommented:
Hi,
you should move the variables inside the function, otherwise they will be set too early in the process:
function addNum() {

var firstNumber = parseInt(document.getElementById('txtNum1').value);
var secondNumber = parseInt(document.getElementById('txtNum2').value);

        var result = firstNumber + secondNumber;

            resultMessage = "Your answer is: " + result;
            ShowResults.innerHTML = resultMessage;

}

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.