Getting calculated value from table data into an input field for form submission

Viewed 16

I found this code which i want to modify a bit:

I have a simple calculation going on in a table. All i want to do is to get the value from my id #total (which is calculated and inserted via js) to be inserted/copied/mirrored into an input field with the id #summe (top left) so it can be submitted with a form.

this is what i have:

$(document).ready(function() {
    $(".order-entry").on("keyup", ".form-calc", function() {
        var parent = $(this).closest("tr");
        parent.find(".form-line").val((parent.find(".quantity").val() * parent.find(".cost").val()).toFixed(2));
        var total = 0;
        $(".form-line").each(function(){
            total += parseFloat($(this).val()||0);
        });
        $("#total").text(total.toFixed(2));
         $("#summe").text(total.toFixed(2));
       
    });
});

function addCommas(n){
    var s=n.split('.')[1];
    (s) ? s="."+s : s="";
    n=n.split('.')[0]
    while(n.length>3){
        s=","+n.substr(n.length-3,3)+s;
        n=n.substr(0,n.length-3)
    }
    return n+s
}
     table {
  margin: 0 auto;
}
td, th {
  margin: .5em 0;
  font-size: 1em;
}
input {
  height: 30px;
  font-size: .8em;
  padding: .5em;
}
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table class="order-entry">
          <thead>
            <tr>
              <th>Quantity</th>
              <th>Price</th>
              <th>Sum</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><input class="form-calc quantity" value="1" /></td>
              <td><input class="form-calc cost" value="10.00" /></td>
              <td><input class="form-line" value="0" readonly /></td>
            </tr>
            <tr>
              <td><input class="form-calc quantity" value="2" /></td>
              <td><input class="form-calc cost" value="5.33" /></td>
              <td>
                <input
                  class="form-line"
                  value="0"
                  readonly
                  onchange="this.value=addCommas(this.value);"
                />
              </td>
            </tr>
            <tr>
              <td colspan="1">Total:</td>
              <td id="total"></td>
            </tr>
          </tbody>
          
          <input  
                  id="summe"
                  class="form-line"
                  value="0"
                  readonly
                  onchange="this.value=.text(total.toFixed(2));"
                />
          

Thank you

0 Answers
Related