1

I am quite new to jquery and am gradually working through my book! I have generated this simple calculator for some form field values. Basically if the user select percentage a pecentage calculation occurs and the results are put into another form field. If the user selects mvalue the script just places that value into the form field instead. The percentage calculator works perfectly, the problem is with the second part of the script, the error message in firefox is calcVal.toFixed is not a function. My apologies if this code is overkill or chunky, but as i say i am still learning.

<script type="text/javascript">
         function myCalc() {
        var selectVal = $('##txt_passoc_type#currentrow# :selected').val();
        var valBox = $('##mon_value#currentrow#').val();
        var currentPrice = $('##currentPrice#currentrow#').val();
        if (selectVal == "Percentage"){
        var calcVal = ((currentPrice * valBox)/100);
        var myTotal = currentPrice - calcVal;
        var myCleanTotal = myTotal.toFixed(2);
         $('##mon_paasoc_dprice#currentrow#').val(myCleanTotal);
         $('##screenPrice#currentrow#').val(myCleanTotal);
     }
    else if (selectVal == "MValue"){
        var myVal = $('##mon_value#currentrow#').val();
        var calcVal = myVal
        var myTot = calcVal.toFixed(2);
        $('##mon_paasoc_dprice#currentrow#').val(myVal);
         $('##screenPrice#currentrow#').val(myVal);


    }

         }

    </script>

Any help is appreciated.

Jason

2 Answers 2

2

the problem is that you are calling toFixed() on a String and it can obnly be called on a number

For example this gives an error

var calcVal = "3";
var myTot = calcVal.toFixed(2);

And this doesn't

var calcVal = "3";
calcVal = parseInt(calcVal , 10);
 var myTot = calcVal.toFixed(2);

So in your example you should do:

else if (selectVal == "MValue"){
    var myVal = $('##mon_value#currentrow#').val();
    var calcVal = parseInt(myVal, 10);//convert to a Number!
    var myTot = calcVal.toFixed(2);
    $('##mon_paasoc_dprice#currentrow#').val(myVal);
     $('##screenPrice#currentrow#').val(myVal);
}

You don't need to do anything in the if part because calcVal is a number

 var calcVal = ((currentPrice * valBox)/100);//Here calcVal is a number already
 var myTotal = currentPrice - calcVal;
Sign up to request clarification or add additional context in comments.

1 Comment

awesome, thank you! been pulling my hair out! apart from that is the rest of code looking ok i.e not to heavy etc.
2

It might be helpful if you posted your html along with the javascript, however I suspect you are calling toFixed on a string. You should try this instead:

parseFloat(calcVal).toFixed(2)

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.