0

I want to calculate division of 2 TextBoxes's value (that shouldn't be empty) and show result in another new TextBox. How can i do that with jquery?

These are my code that don't work:

 $(document).ready(function () { 

    var payNumber=  $('#InsPayNumber').val();
    var insurancePrice=  $('#InsurerInsPrice').val();


    if (payNumber!=null && insurancePrice!=null) {

        if (  $('#HasInsPayWitTax').checked) {

            $("#InsPayInstallmentPrice").val( ( insurancePrice*3/100)/payNumber);
        }
        $("#InsPayInstallmentPrice").val( insurancePrice/payNumber);

    }else {
        $("#InsPayInstallmentPrice").val("");
    } 

});

Thanks.

mortazavi
  • 401
  • 9
  • 22

3 Answers3

0

You need to do the calculation on change event handler

$(document).ready(function () {
    $('#InsPayNumber, #InsurerInsPrice, #HasInsPayWitTax').change(function () {
        var payNumber = parseFloat($('#InsPayNumber').val());
        var insurancePrice = parseFloat($('#InsurerInsPrice').val());
        if (payNumber && insurancePrice) {
            if ($('#HasInsPayWitTax').is(':checked')) {
                $("#InsPayInstallmentPrice").val((insurancePrice * 3 / 100) / payNumber);
            } else {
                $("#InsPayInstallmentPrice").val(insurancePrice / payNumber);
            }
        } else {
            $("#InsPayInstallmentPrice").val("");
        }
    }).change()
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

use this to check checkbox is checked or not

if (payNumber != '' && insurancePrice != '') {

    if($('#HasInsPayWitTax').prop('checked')) {
        // checked
    } else {
        // not checked
    }
}

Update:- more ways to find How to check whether a checkbox is checked in jQuery?

Community
  • 1
  • 1
Ihsahs
  • 892
  • 9
  • 23
0

fiddle

$(document).ready(function () { 
    $('#InsPayNumber, #InsurerInsPrice, #HasInsPayWitTax').change(function () {
    var payNumber=  $('#InsPayNumber').val();
    var insurancePrice=  $('#InsurerInsPrice').val();


    if (payNumber!=null && insurancePrice!=null) {

        if (  $('#HasInsPayWitTax').checked) {

            $("#InsPayInstallmentPrice").val( ( insurancePrice*3/100)/payNumber);
        }else{
           $("#InsPayInstallmentPrice").val( insurancePrice/payNumber);
        }

    }else {
        $("#InsPayInstallmentPrice").val("");
    } 
 });

});
Harish Singh
  • 3,359
  • 5
  • 24
  • 39