3

I am using the following

var loan = parseFloat(document.getElementById("loanamount"));

document.getElementById('numpay').textContent = loan.toString();

and my html is this:

<p>Number of Payments:    <a id="numpay"> </a> </p>

I feel like this should be working but cannot seem to get anything other than NaN in my html, no matter how I configure it. I know I am a novice at javascript but could you please give me a tip?

Thanks!

3
  • 3
    what is the tag with id loanamount? Commented Feb 1, 2013 at 17:51
  • 2
    Why are you parsing a float and then stringifying it? Commented Feb 1, 2013 at 17:55
  • just curious but I would imagine the value you're pulling back is a string as you are parsing it with parseFloat. Then in the next line you're calling .toString(). Seems a little counter productive. Commented Feb 1, 2013 at 17:55

5 Answers 5

3

You need parseFloat(document.getElementById("loanamount").value) most probably

TIP: Instead of parseFloat, just use + to convert from strings to numbers. So +document.getElementById("loanamount").value should also serve your purpose.

Sign up to request clarification or add additional context in comments.

1 Comment

This was a very complete answer and thus I accept it, Thanks!
1
var loan = parseFloat(document.getElementById("loanamount").value);

document.getElementById('numpay').textContent = loan.toString();

1 Comment

This fixed my problem, I will look into javascripts .value more, thank you.
0

If you aren't doing anything with loan then just do this:

document.getElementById('numpay').textContent = document.getElementById("loanamount").value

Comments

0

If the element with id loanamount is an input you will need .getElementById("loanamount").value, if it is a span or div then .getElementById("loanamount").innerHTML. For writing back the same apply.

But in either case you must not use .toString() on the write in. parseFloat() will give you a number, which does not have methods. JavaScript is not that similar to Java.

Comments

0
  var loan = parseFloat(document.getElementById("loanamount").value);

  document.getElementById('numpay').innerHTML = loan.toFixed(2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.