[Solved] Calculate percentage with JavaScript


I run your code on my local env and then knew why NaN exception thrown out. The variable total is retrieved from html input element value according to your code above, but total variable will be string empty before executed line parseInt(cash)+parseInt(checks)+parseInt(coin);. After that, parseInt will return NaN after parse empty string to int. So you will see NaN on percentage input box.

There is a solution to fix this issue: Code as below:

var goal = document.getElementById('goalamount').value;

document.getElementById('a4').value = parseInt(cash)+parseInt(checks)+parseInt(coin);

var total = document.getElementById('a4').value;

solved Calculate percentage with JavaScript