2

This is my script:

<script>
jQuery(document).ready(function () {
    jQuery('#btnCalculate').click(function () {
        var salaries = parseInt(jQuery('#txtEmployeeSalaries').val(), 10);
        var nationalInsurance = parseInt(jQuery('#txtENIC').val(), 10);
        var pensionCont = parseInt(jQuery('#txtEPC').val(), 10);
        var expenses = parseInt(jQuery('#txtAnyExpenses').val(), 10);
        var income = parseInt(jQuery('#txtIncome').val(), 10);

        var labourCost = (((salaries + nationalInsurance + pensionCont + expenses) / (income)) * 100);
        alert(labourCost);
        jQuery('#txtTotal').val(labourCost).toFixed(2);
    });
});
</script>

However in the Chrome Console it states:

Uncaught TypeError: Object [object Object] has no method 'toFixed'

Anyone see anything obviously wrong with this?

Subby
  • 5,370
  • 15
  • 70
  • 125

2 Answers2

7

use toFixed this way - (You are getting that error bcoz you are trying to use that method on jquery object)

jQuery('#txtTotal').val(labourCost.toFixed(2));
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
3

You're putting toFixed() in the wrong place. (toFixed() works on numbers, yet you've applied it to the jQuery object instead of the number in labourCost.) Use:

jQuery('#txtTotal').val(labourCost.toFixed(2));
Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
  • Can you also tell me what the Radix is meant to do in PareInt? – Subby May 28 '13 at 10:39
  • 1
    @Subby its to avoid on older browser to parse to base 8 integer prefixed with a 0. – A. Wolff May 28 '13 at 10:40
  • 1
    The radix is the second value you pass to `.parseInt()`, and it determines which numerical base you are using. So, `.parseInt(number, 10)` will parse `number` into base-10 (decimal). It's always recommended you specify the radix in order to avoid confusion as to the base. – Derek Henderson May 28 '13 at 10:41
  • Ah ok. That makes perfect sense. Thank you both very much. – Subby May 28 '13 at 10:45