Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:56:16+00:00 2026-06-13T09:56:16+00:00

I want to create a stock calculation system with javacript now i have create

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:56:17+00:00Added an answer on June 13, 2026 at 9:56 am

    I recommend you to keep the logic in JavaScript and keep the HTML clean! 🙂

    Instead of onClic you could use onChange

    <input onChange="program(this)" />
    

    Next, we will calculate everything in JavaScript…

    function program(el) {
      var fields = document.stock;
    
      // Do nothing, if the required fields are empty
      if (fields.t1.value == '' || fields.t2.value == '') return;
    
      // Calculate result
      fields.t3.value = fields.t1.value - fields.t2.value;
    
      // Do nothing, if the required fields are empty
      if (fields.t4.value == '' || fields.t5.value == '') return;
    
      // Calculate result
      fields.t6.value = fields.t4.value - fields.t5.value;
    
      // Do nothing, if the required fields are empty
      if (fields.t7.value == '' || fields.t8.value == '') return;
    
      // Calculate result
      fields.t9.value = fields.t7.value - fields.t8.value;
    }
    

    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:

    <form name="stock">
      <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>
    
        <tr align="center">
          <td><input id="t1" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t2" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t3" type="text" style="width:260px;" class="input1" readonly /></td>
        </tr>
        <tr align="center">
          <td><input id="t4" type="text" style="width:260px;" class="input1" readonly /></td>
          <td><input id="t5" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t6" type="text" style="width:260px;" class="input1" readonly /></td>
        </tr>
        <tr align="center">
          <td><input id="t7" type="text" style="width:260px;" class="input1" readonly /></td>
          <td><input id="t8" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t9" type="text" style="width:260px;" class="input1" readonly /></td>
        </tr>
      </table>
    </form>
    

    Notice that I did set t4 and t7 fields as readonly.

    And the JavaScript:

    function program(el) {
      var fields = document.stock;
    
      // Clear the calculation fields
      fields.t3.value = '';
      fields.t6.value = '';
      fields.t9.value = '';
      fields.t4.value = '';
      fields.t7.value = '';
    
      // Do nothing, if any of the required fields are empty
      if (fields.t1.value === '' || fields.t2.value === '') return;
    
      // Calculate result
      fields.t3.value = fields.t1.value - fields.t2.value;
    
      // Copy the result
      fields.t4.value = fields.t3.value;
    
      // Do nothing, if the required fields are empty
      if (fields.t5.value === '') return;
    
      // Calculate result
      fields.t6.value = fields.t4.value - fields.t5.value;
    
      // Copy the result
      fields.t7.value = fields.t6.value;
    
      // Do nothing, if the required fields are empty
      if (fields.t8.value === '') return;
    
      // Calculate result
      fields.t9.value = fields.t7.value - fields.t8.value;
    }
    

    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:

    <form name="stock">
      <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>
    
        <tr align="center">
          <td><input id="t1" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t2" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t3" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
        </tr>
        <tr align="center">
          <td><input id="t4" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t5" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t6" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
        </tr>
        <tr align="center">
          <td><input id="t7" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t8" type="text" style="width:260px;" class="input1" onChange="program(this)" /></td>
          <td><input id="t9" type="text" style="width:260px;" class="input1" readonly="readonly" /></td>
        </tr>
      </table>
    </form>
    

    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.

    function program(el) {
      // Get the row (the <tr> element)
      var row = el.parentNode.parentNode;
    
      // Get all the <input> elements in row
      var inputs = row.getElementsByTagName('input');
    
      // Return, if required fields are empty
      if (inputs[0].value === '' || inputs[1].value === '') {
        inputs[2].value = '';
        return;
      }
    
      // Calculate the result
      inputs[2].value = inputs[0].value - inputs[1].value;
    
      // Find the next row (<tr> element)
      var nextRow = row.nextSibling.nextSibling;
      while (nextRow !== null && nextRow.tagName !== 'TR') {
        nextRow = nextRow.nextSibling;
      }
    
      // Return, if the next row doesn't exist
      if (nextRow === null || nextRow.tagName !== 'TR') return;
    
      // Get all <input> fields in row
      var nextRowInputs = nextRow.getElementsByTagName('input');
    
      if (nextRowInputs[0].value === '') {
        // Copy the previous row result into next rows first input,
        // only if it's not filled already
        nextRowInputs[0].value = inputs[2].value;
      }
    }
    

    And this demo can be tested here: http://jsbin.com/welcome/39893/edit

    Vote this answer up and give me some points, please! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want create image animation , i have 50 images with png format now
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I want to create a new field (or two) in my table that is
I'm a newbie in WPF applications. I want to create a application that displays
So I got a problem with saving my pdf. I want to create an
I want to create a button with the stock Remove icon on it, but
I have a list of values and dates for stock ticker symbols and want
I now have such code: fDeals = fopen([logsFolder stock '_deals.log']); data = textscan(fDeals, '%f:%f:%f:%f
I am writing a stock market system that uses several threads to process the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.