1

I keep on getting this error when using this code. SRC --> http://jsfiddle.net/MYG2C/1/

JS

    <input class="menu1" type="submit" value="Total : 0.00" onclick="m.getTotal()" id="total" />

HTML

var m = {
    total:0,
    getTotal: function () {
        this.total = this.total.toFixed(2);
        document.getElementByID("total").value = "Total : " + this.total;
    },
}
m.total = 5;
//Clicking the button should now update the text on the button.

2 Answers 2

2

Two problems...

  • getElementByID should be getElementById
  • this.total = this.total.toFixed(2); will work the first time because total is a number. After that it becomes a string and toFixed is not a valid method on a string

Updated Fiddle

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

Comments

1

you have a typo as anthony chu mentioned and below is the modified script for check the type and avoid the Uncaught TypeError.

var m = {
    total:8,
    getTotal: function () {
        if(typeof this.total == "number")
        {
        console.log("executed");
        this.total = this.total.toFixed(2);
        document.getElementById("total").value = "Total : " + this.total;
        }
    },
}

JSFIDDLE : http://jsfiddle.net/8J2G7/4/

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.