I'm trying to use the product of one function to be used later on in my program.
I have the following code:
var calculate = function(ingVolume, percentCarrier, totalCarrierVolume, totalIngredientVolume) {
var volume = document.getElementById(ingVolume).value;
var carrier = document.getElementById(percentCarrier).value;
var mCarrier = carrier/100;
var x = document.getElementById(totalCarrierVolume).innerHTML = mCarrier*(volume/(1-
mCarrier));
var y = document.getElementById(totalIngredientVolume).innerHTML = parseInt(volume) + x;
return [x, y];
};
var value1 = calculate('a', 'b', 'c', 'd');
var subvalue1_1 = value1[0];
var subvalue1_2 = value1[1];
var value2 = calculate('e', 'f', 'g', 'h');
var subvalue2_1 = value2[0];
var subvalue2_2 = value2[1];
var value3 = calculate('i', 'j', 'k', 'l');
var subvalue3_1 = value3[0];
var subvalue3_2 = value3[1];
var value4 = calculate('m', 'n', 'o', 'p');
var subvalue4_1 = value4[0];
var subvalue4_2 = value4[1];
var value5 = calculate('q', 'r', 's', 't');
var subvalue5_1 = value5[0];
var subvalue5_2 = value5[1];
function calculate2() {
document.getElementById("demo").innerHTML = value1;
};
If I plug some numbers into this and run it, the variable 'value1' is returned as undefined. However, when I run calculate('a', 'b', 'c', 'd') in the javascript console it runs fine and returns [value of x, value of y] for those parameters.
The exact error message is
"Uncaught TypeError: Cannot read property 'value' of null" for line 17
which is var volume = document.getElementById(ingVolume).value;
It is being run for the following html:
<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('a', 'b', 'c', 'd')">Calculate Final
Volume</button></td>
<td id="c"></td>
<td id="d"></td>
<td><a href="#" onclick="toggleRow('row2')">New Ingredient</a></td>
</tr>
<tr id="row2" style="display: none;">
<td><input type="text"></input></td>
<td><input type="number" id="e"> Mg</input></td>
<td><input type="number" id="f"></input> %</td>
<td><button type="button" onclick="calculate('e', 'f', 'g', 'h')">Calculate Final
Volume</button></td>
<td id="g"></td>
<td id="h"></td>
<td><a href="#" onclick="toggleRow('row3')">New Ingredient</a></td>
<td><a href="#" onclick="toggleRow('row2')">Delete Ingredient</a></td>
</tr>
<tr id="row3" style="display: none;">
<td><input type="text"></input></td>
<td><input type="number" id="i"> Mg</input></td>
<td><input type="number" id="j"></input> %</td>
<td><button type="button" onclick="calculate('i', 'j', 'k', 'l')">Calculate Final
Volume</button></td>
<td id="k"></td>
<td id="l"></td>
<td><a href="#" onclick="toggleRow('row4')">New Ingredient</a></td>
<td><a href="#" onclick="toggleRow('row3')">Delete Ingredient</a></td>
</tr>
<tr id="row4" style="display: none;">
<td><input type="text"></input></td>
<td><input type="number" id="m"> Mg</input></td>
<td><input type="number" id="n"></input> %</td>
<td><button type="button" onclick="calculate('m', 'n', 'o', 'p')">Calculate Final
Volume</button></td>
<td id="o"></td>
<td id="p"></td>
<td><a href="#" onclick="toggleRow('row5')">New Ingredient</a></td>
<td><a href="#" onclick="toggleRow('row4')">Delete Ingredient</a></td>
</tr>
<tr id="row5" style="display: none;">
<td><input type="text"></input></td>
<td><input type="number" id="q"> Mg</input></td>
<td><input type="number" id="r"></input> %</td>
<td><button type="button" onclick="calculate('q', 'r', 's', 't')">Calculate Final
Volume</button></td>
<td id="s"></td>
<td id="t"></td>
<td><a href="#" onclick="noMoreRows()">New Ingredient</a></td>
<td><a href="#" onclick="toggleRow('row5')">Delete Ingredient</a></td>
</tr>
</table>
</form>
<button type="button" onclick="calculate2()">Click Me</button>
<p id="demo"></p>
I want to eventually use subvalue1_1 and subvalue1_2 for additional calculations by other functions, but it won't work if there's no value stored for value1.
The html appears after the javascript, which is in the head of the html document.