1

I am new to jQuery and javascript, I need a help on how to do computation on form text field in an array. See below sample code;

<head lang="en">
  <meta chartaxt="UTF-8">
  <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <script src="js/jquery.min.js"></script>
  <script src="bootstrap/js/bootstrap.min.js"></script>
  <title>Sample salary computation</title>

</head>

<body>

  <form method="POST" name="regis" id="regis">
    <table align="center" border="1" width="200">
      <tr>
        <th>Staff ID</th>
        <th>Salary</th>
        <th>Total Tax</th>
        <th>Total Net Pay</th>
      </tr>

      <!--Staff 1-->
      <tr data-id="STAFF/2016/001">
        <td>
          <input type="text" name="staffid[]" class="staffid" value="STAFF/2016/001" readonly="readonly">
        </td>
        <td>
          <input type="text" name="salary[]" class="salary" placeholder="salary" value="5000">
        </td>
        <td>
          <input type="text" name="tax[]" class="tax" placeholder="tax" value="500">
        </td>
        <td>
          <input type="text" name="total[]" class="total" placeholder="total" readonly="readonly">
        </td>
      </tr>

      <tr data-id="STAFF/2016/002">
        <td>
          <input type="text" name="staffid[]" class="staffid" value="STAFF/2016/002" readonly="readonly">
        </td>
        <td>
          <input type="text" name="salary[]" class="salary" placeholder="salary" value="500">
        </td>
        <td>
          <input type="text" name="tax[]" class="tax" placeholder="tax" value="50">
        </td>
        <td>
          <input type="text" name="total[]" class="total" placeholder="total" readonly="readonly">
        </td>
      </tr>


      <tr data-id="STAFF/2016/003">
        <td>
          <input type="text" name="staffid[]" class="staffid" value="STAFF/2016/003" readonly="readonly">
        </td>
        <td>
          <input type="text" name="salary[]" class="salary" placeholder="salary" value="5000">
        </td>
        <td>
          <input type="text" name="tax[]" class="tax" placeholder="tax" value="600">
        </td>
        <td>
          <input type="text" name="total[]" class="total" placeholder="total" readonly="readonly">
        </td>
      </tr>
    </table>
  </form>
</body>

I want a jquery script that can sum total = (salary + tax) for each staff using onclick button

3 Answers 3

1

$("#submit").click(function() {
  $("tr").each(function() {
    if ($(this).find(".salary")) {
      var salary = parseInt($(this).find(".salary").val(), 10)
      var tax = parseInt($(this).find(".tax").val(), 10)
      $(this).find(".total").val(salary + tax)
    }
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" name="regis" id="regis">
  <table align="center" border="1" width="200">
    <tr>
      <th>Staff ID</th>
      <th>Salary</th>
      <th>Total Tax</th>
      <th>Total Net Pay</th>


    </tr>

    <!--Staff 1-->
    <tr data-id="STAFF/2016/001">
      <td>
        <input type="text" name="staffid[]" class="staffid" value="STAFF/2016/001" readonly="readonly">
      </td>
      <td>
        <input type="text" name="salary[]" class="salary" placeholder="salary" value="5000">
      </td>
      <td>
        <input type="text" name="tax[]" class="tax" placeholder="tax" value="500">
      </td>
      <td>
        <input type="text" name="total[]" class="total" placeholder="total" readonly="readonly">
      </td>
    </tr>


    <tr data-id="STAFF/2016/002">
      <td>
        <input type="text" name="staffid[]" class="staffid" value="STAFF/2016/002" readonly="readonly">
      </td>
      <td>
        <input type="text" name="salary[]" class="salary" placeholder="salary" value="500">
      </td>
      <td>
        <input type="text" name="tax[]" class="tax" placeholder="tax" value="50">
      </td>
      <td>
        <input type="text" name="total[]" class="total" placeholder="total" readonly="readonly">
      </td>
    </tr>


    <tr data-id="STAFF/2016/003">
      <td>
        <input type="text" name="staffid[]" class="staffid" value="STAFF/2016/003" readonly="readonly">
      </td>
      <td>
        <input type="text" name="salary[]" class="salary" placeholder="salary" value="5000">
      </td>
      <td>
        <input type="text" name="tax[]" class="tax" placeholder="tax" value="600">
      </td>
      <td>
        <input type="text" name="total[]" class="total" placeholder="total" readonly="readonly">
      </td>
    </tr>

  </table>
  <button id="submit" type="button">Submit</button>

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Harz, I just run the code above, once i click on submit button alert box displays undefined and the total does not show any result
@NelsonOko-oza Are there any errors in your console? I'm confused because it works fine for me.
@arz 4 , The result is not showing inside the total text field. I actually wanted the sum of tax and salary to appear inside the total text field
You mean inside the "Total Net Pay" field or the "Total Tax" field?
Yes @arz 4, I want the result to appear inside "Total Net Pay" field
|
0

You can use selector to get or set the val with iQuery. Something like this

$("button").click(function(){
    var total = $("input[name=salary]").val()+$("input[name=tax]").val();
    $("input[name=total]").val(tatal);
  });

Comments

0

Here is one solution:

http://codepen.io/tryggvigy/pen/BzAqGR

var sums = []
$('input[type=button]').click(function() {
  var salaries = $('.salary')
    .toArray()
    .map(function(salary) { return salary.value })
  var taxes = $('.tax')
    .toArray()
    .map(function(tax) { return tax.value})
  sums = salaries.map(function(salary, i) {
    return parseInt(salary) + parseInt(taxes[i])
  })
  alert(document.sums)
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.