I am building in .net-mvc and I will like to substract two numbers in a textbox and display in another textbox all in the View (cshtml). I have tried all ways but can't figure out why it will not work.
var a = parseInt(document.getElementById("sale").value);
var b = parseInt(document.getElementById("cost").value);
var c;
a.onkeyup = function() {
var c;
if (isNaN(a.value) == false) {
c = (parseInt(a.value) || 0) - (parseInt(b.value) || 0);
document.getElementById("profit").value = c;
}
}
b.onkeyup = function() {
if (isNaN(b.value) == false) {
c = (parseInt(a.value) || 0) - (parseInt(b.value) || 0);
document.getElementById("profit").value = c;
}
}
<div class="form-style-5">
<form method="get" action="InsertBattCalcDetails">
<fieldset>
<legend><span class="number">1</span> Insert Battery Calculations</legend>
Date: <input type="date" name="Date" /> <br /> Battery Value: <input type="number" name="Batt_value" /> <br /> Battery Sales: <input type="text" name="Batt_sales" id="sale" /> <br /> Battery Cost: <input type="text" name="Batt_cost" id="cost" /> <br /> Battery Profit: <input type="text" name="Batt_profit" id="profit" /> <br /> Battery Bought: <input type="number" name="Batt_bought" /> <br />
</fieldset>
<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
</form>
</div>
asupposed to be? You initialise it asparseInt(document.getElementById("sale").value);which means it will be a number but then use use it as if it's an element by callinga.onkeyupanda.value. Same withb