I want to create a stock calculation system with javacript now i have create it there is some problem the problem is that i have created 3 rows 1.Purchase,2.Sold,3.Stock.Now i have set that when i give purchase value and sold quantity it will subtract and print value in stock text box and then is next column the stock value also prints in purcahse text box the problem is when the purchase item become nill means 0 so what can i do to add some quantity and start again this process.
the coding is.
<html>
<head>
<title>Stock Information</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script type="text/javascript">
//------------------------------------Date---------------------------------------------
var today = new Date(); //
var dd = today.getDate(); //
var mm = today.getMonth()+1; //January is 0! //
var yyyy = today.getFullYear(); //
today = dd+'/'+mm+'/'+yyyy; //
document.write(today); //
//-------------------------------------------------------------------------------------
//----------------------------------------------------
function program(a,b,c,d,e,f)
{
var total1=a-b;
document.stock.t3.value=total1;
var jump1=document.stock.t3.value;
document.stock.t4.value=jump1;
var total2= c-d;
document.stock.t6.value=total2;
var jump2=document.stock.t6.value;
document.stock.t7.value=jump2;
var total3= e-f;
document.stock.t9.value=total3;
}
</script>
</head>
<body>
<center>
<h1 class="table">Stock Information</h1>
</center>
<table width="800" align="center" class="table1">
<tr>
<th width="260"class="table">Purchase</th>
<th width="260"class="table">Sold</th>
<th width="428"class="table">Stock</th>
</tr>
<form name="stock">
<tr align="center">
<td><input id="t1" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t2" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t3" type="text" style="width:260px;" class="input1" onClick="program(t1.value,t2.value)" readonly/></td>
</tr>
<tr align="center">
<td><input id="t4" type="text" style="width:260px;" class="input1" onClick="program(t1.value,t2.value,t4.value,t5.value)"/></td>
<td><input id="t5" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t6" type="text" style="width:260px;" class="input1" onClick="program(t1.value,t2.value,t4.value,t5.value)" readonly/></td>
</tr>
<tr align="center">
<td><input id="t7" type="text" style="width:260px;" class="input1" onClick="program(value1.value,value2.value,t4.value,t5.value)"/></td>
<td><input id="t8" type="text" style="width:260px;" class="input1"/></td>
<td><input id="t9" type="text" style="width:260px;" class="input1" onClick="program(value1.value,value2.value,t4.value,t5.value,t7.value,t8.value)"readonly/></td>
</tr>
</form>
</table>
</body>
</html>
I recommend you to keep the logic in JavaScript and keep the HTML clean! 🙂
Instead of
onClicyou could useonChangeNext, we will calculate everything in JavaScript…
I don’t understand exactly how it suppose to work, , but I will try to repeat the same logic, what you did.
I changed the HTML structure a little:
Notice that I did set
t4andt7fields asreadonly.And the JavaScript:
You can also look at this demo in JSBin: http://jsbin.com/welcome/39835/edit
I hope I got it right.
UPDATE
OK, I did some improvements. But instead of checking if the result is
0, I just allow to write in first place. That function when the result gets copied to the next row’s first field, now is just a helper and will not overwrite the value, if the field is not empty. I think that’s how it should be.Slightly changed HTML:
And, most importantly, JavaScript! There is a DOM traversing involved, so you don’t need depend on the
id=""anymore AND you can create as many rows you need without changing the JavaScript code.And this demo can be tested here: http://jsbin.com/welcome/39893/edit
Vote this answer up and give me some points, please! 🙂