This topic is locked
[SOLVED]

 Question: Calculate on the fly with Radio Button

10/26/2011 11:56:28 PM
PHPRunner General questions
D
direwolf author

I've been experimenting on this for hours now and nothing seems to work.
Background:

  • I have a form that have auto calculate fields, which gets the values from text fields (working fine)
  • I am editing the code in JavaScript OnLoad event
  • I've been using the variable.on('keyup', functionname) method and it's working for text fields
    Problem:
  • I also have radio buttons (e.g. tax included or excluded)
  • So upon toggling the radio buttons, it does not automatically calculate the fields
  • The algorithm / value getting seems to be working because tax is computed whenever I change any of the textfields but it does not auto calculate when I change the radio button
    Code Snippet:

    casePrice.on('keyup', funcCase);

    caseQuantity.on('keyup', funcCase);

    productDisc1.on('keyup', funcCase);

    productDisc2.on('keyup', funcCase);

    productDisc3.on('keyup', funcCase);

    boxQuantity.on('keyup', funcCase);

    <-- this the radio button and it seems to be the problem, I've tried onclick, onchange.. nothing auto calculates upon changing the radio
    Summary:
  • Values are auto-calculated whenever I change the text fields
  • Values are NOT auto-calculated whenever I change the radio button

    --- Although calculations will still be correct calculated after I change a textfield after changing the radio button

C
cgphp 10/27/2011

Post also the code of the funcCase function.

D
direwolf author 10/27/2011

Code is below.

  • The produtVat is the radio button... if it's Inclusive, it no longer adds VAT to the cost. Otherwise, it will add VAT.
  • Changing the values of the radio button does not trigger any calculation, but changing text fields does (it even gets the current value of the radio)
    ---------------------------
    function funcCase() {
    if ( productVat.getValue() == "Inclusive")
    {

    if ( casePrice.getValue()!='' && !isNaN(casePrice.getValue()))

    {

    var caseCostVar = ((parseFloat(casePrice.getValue()) ((100 - parseFloat(productDisc1.getValue())) / 100)) ((100 - parseFloat(productDisc2.getValue())) / 100) ) ((100 - parseFloat(productDisc3.getValue())) / 100);

    caseCost.setValue(caseCostVar.toFixed(2));
    if ( caseQuantity.getValue()!='0' && !isNaN(caseQuantity.getValue()) && caseQuantity.getValue()!='')

    {

    var piecePriceVar = (parseFloat(casePrice.getValue()) / parseFloat(caseQuantity.getValue()));

    piecePrice.setValue(piecePriceVar.toFixed(2));

    var pieceCostVar = ((parseFloat(piecePrice.getValue())
    ((100 - parseFloat(productDisc1.getValue())) / 100)) ((100 - parseFloat(productDisc2.getValue())) / 100) ) ((100 - parseFloat(productDisc3.getValue())) / 100);

    pieceCost.setValue(pieceCostVar.toFixed(2));
    if ( boxQuantity.getValue()!='0' && !isNaN(boxQuantity.getValue()) && boxQuantity.getValue()!='')

    {

    var boxPriceVar = (parseFloat(boxQuantity.getValue()) parseFloat(piecePrice.getValue()));

    boxPrice.setValue(boxPriceVar.toFixed(2));

    var boxCostVar = ((parseFloat(boxPrice.getValue())
    ((100 - parseFloat(productDisc1.getValue())) / 100)) ((100 - parseFloat(productDisc2.getValue())) / 100) ) ((100 - parseFloat(productDisc3.getValue())) / 100);

    boxCost.setValue(boxCostVar.toFixed(2));

    }

    else

    {

    boxPrice.setValue('0.00');

    }

    }

    else

    {

    piecePrice.setValue('0.00');

    }

    }

    else
    {

    caseCost.setValue('');

    }
    }
    if ( productVat.getValue() == "Exclusive")
    {

    if ( casePrice.getValue()!='' && !isNaN(casePrice.getValue()))

    {

    var caseCostVar = ((parseFloat(casePrice.getValue()) ((100 - parseFloat(productDisc1.getValue())) / 100)) ((100 - parseFloat(productDisc2.getValue())) / 100) ) ((100 - parseFloat(productDisc3.getValue())) / 100);

    var caseCostVat = (caseCostVar
    1.12);

    caseCost.setValue(caseCostVat.toFixed(2));

    var caseVatVar = (caseCostVar .12);

    caseVat.setValue(caseVatVar.toFixed(2));
    if ( caseQuantity.getValue()!='0' && !isNaN(caseQuantity.getValue()) && caseQuantity.getValue()!='')

    {

    var piecePriceVar = (parseFloat(casePrice.getValue()) / parseFloat(caseQuantity.getValue()));

    piecePrice.setValue(piecePriceVar.toFixed(2));

    var pieceCostVar = ((parseFloat(piecePrice.getValue())
    ((100 - parseFloat(productDisc1.getValue())) / 100)) ((100 - parseFloat(productDisc2.getValue())) / 100) ) ((100 - parseFloat(productDisc3.getValue())) / 100);

    var pieceCostVat = (pieceCostVar 1.12);

    pieceCost.setValue(pieceCostVat.toFixed(2));

    var pieceVatVar = (pieceCostVar
    .12);

    pieceVat.setValue(pieceVatVar.toFixed(2));
    if ( boxQuantity.getValue()!='0' && !isNaN(boxQuantity.getValue()) && boxQuantity.getValue()!='')

    {

    var boxPriceVar = (parseFloat(boxQuantity.getValue()) parseFloat(piecePrice.getValue()));

    boxPrice.setValue(boxPriceVar.toFixed(2));

    var boxCostVar = ((parseFloat(boxPrice.getValue())
    ((100 - parseFloat(productDisc1.getValue())) / 100)) ((100 - parseFloat(productDisc2.getValue())) / 100) ) ((100 - parseFloat(productDisc3.getValue())) / 100);

    var boxCostVat = (boxCostVar 1.12);

    boxCost.setValue(boxCostVat.toFixed(2));

    var boxVatVar = (boxCostVar
    .12);

    boxVat.setValue(boxVatVar.toFixed(2));

    }

    else

    {

    boxPrice.setValue('0.00');

    }

    }

    else

    {

    piecePrice.setValue('0.00');

    }

    }

    else
    {

    caseCost.setValue('');

    }
    }
    };

C
cgphp 10/27/2011

Are you sure that

productVat.on('click', funcCase);



doens't work ?
Do you get some error from firebug ?

D
direwolf author 11/1/2011



Are you sure that

productVat.on('click', funcCase);



doens't work ?
Do you get some error from firebug ?


It's working now. Thanks man!
It worked after I full build. It didn't work if only partial build.
And I was using onclick. not .on('click'...