I am trying to create an html form that makes calculations based off of user input.
This is the code I have right now:
<form name="testing">
<table border="1" style="padding: 5px;">
<tr>
<td>Ingredient Name</td>
<td>Amount (in Mg)</td>
<td>% Carrier</td>
<td></td>
<td>Total Carrier Volume</td>
<td>Total Ingredient Volume</td>
</tr>
<tr>
<td><input type="text"></input></td>
<td><input type="number" id="a"> Mg</input></td>
<td><input type="number" id="b"></input> %</td>
<td><button type="button" onclick="calculate()">Calculate Final Volume</button></td>
<td id="c"></td>
<td id="d"></td>
</tr>
</table>
</form>
With the following javascript:
<script>
function calculate() {
var volume = document.testing.a.value;
var carrier = document.testing.b.value;
var mCarrier = carrier/100;
var x = document.getElementById("c").innerHTML = mCarrier*(volume/(1-mCarrier));
document.getElementById("d").innerHTML = volume + x;
}
</script>
You can also see it here: www.healthkismet.com/supplement_pricer.html
Right now the line:
document.getElementById("d").innerHTML = volume + x
evaluates to string + x, but I am not sure why. If I do calculations with just x then everything is okay.
I think the way I'm storing the variables 'volume' and 'carrier' is the root cause but I don't know why. (and the 'x' variable is computed as an integer because it's already been divided and multiplied).
I've looked at this answer on stackexchange: Addition is not working in JavaScript and understand how to hack around it.
I've also seen this answer: take user input from a Form perform calculation (order of operations) and output back into that form but if I just typed that example in verbatim I wouldn't know exactly why my code doesn't work right now as it is.