1

Trying to using jQuery do some basic math functions.

Step 1. User enter Gross weight, and Tare weight, my function auto generate Net weight, which is Gross weight minus Tare weight.(This step works)

Step 2. User enter Price per pound, my function use Price per pound times Net weight to calculate total cost.

So far my code looks like this, it returns the net weight but does not return the total cost:

<script>
$(document).ready(function() {
       $(".sub").focusout(function() {
         $("#net").html('');
         var gross = $("#gross").val();
         var tare = $("#tare").val();
         var net = gross-tare;
         $("#net").html(Math.round(net*1000)/1000);
        });
    });

$(document).ready(function() {
       $(".sub1").focusout(function() {
         $("#total").html('');
         var net = $("#net").val();
         var ppp = $("#ppp").val();
         var total = net*ppp;
         $("#total").html(Math.round(total*1000)/1000);
        });
    }); 

  </script>

And the HTML:

   <input type='number' name='gross' id=gross class=sub>
   <input type='number' name='tare' id=tare class=sub>
   <p id=net name='net'></p>
   <input type="number" id=ppp name="ppp" class=sub1/>
   <p id=total name='total'></p>

Forgive me if I am making stupid mistakes as I barely know jQuery, this is just one requirement for my project that I am working on.

2 Answers 2

1

A few minor things:

-id=gross should have quotes id='gross'

-$("#net").val(); net doesn't have a value, its a p, so use text()

-you'll want to use parseInt or parseFloat

Example:

$(document).ready(function () {
    $(".sub").focusout(function () {
        $("#net").html('');
        var gross = $("#gross").val();
        var tare = $("#tare").val();
        var net = gross - tare;
        $("#net").html(Math.round(net * 1000) / 1000);
    });

    $(".sub1").focusout(function () {
        $("#total").html('');
        var net = parseInt($("#net").text());
        var ppp = parseInt($("#ppp").val());
        var total = net * ppp;
        $("#total").html(Math.round(total * 1000) / 1000);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type='number' name='gross' id='gross' class='sub'/>
<input type='number' name='tare' id='tare' class='sub'/>
<p id='net' name='net' class='sub1'></p>
<input type="text" id='ppp' name="ppp" class='sub1'/>
<p id='total' name='total'></p>

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

1 Comment

"you'll want to use parseInt or parseFloat" in most instances I recommend using the unary + operator to cast to a Number instead of parseFloat. It's more concise too: +$('#gross').val().
1

Put the class names/ ids or other attributes in quotes. They are parsed wrongly by the browser. Check using "firebug" or "inspect element".

Problem in your code: You are using '$("#net").val()', but $.val() works only on input fields but #net is a <p> tag, so get its text using $.html().

Your code should be like this

HTML:

<input type='number' name='gross' id=gross class="sub">
<input type='number' name='tare' id=tare class="sub">
<p id="net" name='net'></p>
<input type="number" id="ppp" name="ppp" class="sub1"/>
<p id="total" name='total'></p>

SCRIPT:

$(document).ready(function() {
   $(".sub").focusout(function() {
      $("#net").html('');
      var gross = $("#gross").val();
      var tare = $("#tare").val();
      var net = gross-tare;
      $("#net").html(Math.round(net*1000)/1000);
   });
});

$(document).ready(function() {
   $(".sub1").focusout(function() {
     $("#total").html('');
     var net = parseFloat($("#net").html());
     var ppp = parseFloat($("#ppp").val());
     var total = net*ppp;
     $("#total").html(parseFloat(total));
   });
}); 

enter image description here

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.