Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onclick and function problems

    I'm new to JavaScript programming and have to do a course in it for a surveying degree. I've worked out the logic and it works fine but when it comes to getting the button to call on the function it wont work. I get an error message saying "Object doesn't support this property or method"

    I've called the function:
    Code:
    function calcChange(fmCalc) {
    	// write your code here
    var totalOwes = document.fmCalc.txtTotal;
    var totalPaid = document.fmCalc.txtCash;
    var totalChange = totalPaid - totalOwes;
    totalChange = totalChange.toFixed(2)
    
    
    if (totalChange < 0)
    {
    	alert ("You still owe $" + Math.abs(totalChange))
    }
    and the button is

    Code:
     <input name="btnStart" type="button" id="btnStart" value="Calculate Change" onclick="calcChange(this.Form)"/></td>
    when I change the button's onclick to
    Code:
    <input name="btnStart" type="button" id="btnStart" value="Calculate Change" onclick="calcChange(fmCalc)"/></td>
    it wont work either and comes up with the same error message.

    I've tried everything in my textbook but all the examples pretty much show that the part after "onclick" in the button's code has to be the same as the functions name and it should work. I have no idea what is wrong with it and would greatly appreciate any help. I've attached the txt document so it can be seen what I am trying to do.
    Thanks
    Attached Files Attached Files

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,141
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    var totalOwes = document.fmCalc.txtTotal.value;
    var totalPaid = document.fmCalc.txtCash.value;

    var totalChange = totalPaid - totalOwes;
    totalChange = totalChange.toFixed(2);

    if (totalChange < 0) {
    alert ("You still owe $" + Math.abs(totalChange));
    }

    else {
    alert (totalChange + " Change");
    }

    document.write should be avoided - it erases the current page and creates a new one. In any case your syntax was wrong.
    Last edited by Philip M; 12-16-2007 at 11:41 AM. Reason: Good practice to end lines with a semi-colon ;

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by julianflynn View Post
    Code:
    function calcChange(fmCalc) {
    	// write your code here
    var totalOwes = document.fmCalc.txtTotal;
    var totalPaid = document.fmCalc.txtCash;
    var totalChange = totalPaid - totalOwes;
    totalChange = totalChange.toFixed(2)
    
    
    if (totalChange < 0)
    {
    	alert ("You still owe $" + Math.abs(totalChange))
    }
    - you must close the red bracket
    - is not an error but you don't use anyware the argument fmCalc of the function
    - check to see if the value of totalOwes and totalPaid are corect after click, you can use alert for that

    best regards

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    but the real mistake is that the argument fmCalc is used as a name in the function (and even so, not in a correct way), while it has been passed as an object (and even so not in a correct way - this.Form).

    A lot of other errors (1. the function it is not closed, 2. when subtracting, the values must be parsed into decimal numbers, 3. you must have forgot that toFixed() turns a number into a string thus you can not use Math methods upon it, 4. there is of no use to perform a Math.abs into an alert...... and so on...)
    Try this:
    Code:
    function calcChange(f) {
    var totalOwes = Number(f['txtTotal'].value);
    var totalPaid = Number(f['txtCash'].value);
    var totalChange = totalPaid-totalOwes;
    totalChange = totalChange.toFixed(2);
    if (Number(totalChange)<0){
    alert ("You still owe $ " + totalChange);
    }
    }
    ...
    ...
    <input name="btnStart" type="button" id="btnStart" value="Calculate Change" onclick="calcChange(this.form)"/>
    Last edited by Kor; 12-16-2007 at 12:07 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,141
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Kor View Post
    3. you must have forgot that toFixed() turns a number into a string thus you can not use Math methods upon it,
    I don't agree.

    alert ("You still owe $" + Math.abs(totalChange))

    returns the correct amount.

    <script type = "text/javascript">
    x = 2.3456789;
    y = x.toFixed(2);
    alert (y + " " + typeof y);
    z = Math.ceil(y);
    alert (z + " " + typeof z);
    </script>

    But I agree that there are quite a lot of errors (especially logic errors) other than those I mentioned.
    Last edited by Philip M; 12-16-2007 at 12:19 PM.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Maybe I was vague when said "Math methods", while I should have been said "math anything, mainly math operators like + and -".

    Some math operators needs decimal numbers, some don't. Isn't safer to use always decimal numbers when you do math calculations, and strings in the rest of cases?

    toFixed() transforms a number into a string, that is out of any doubt. What costs you to make it a number when you decide that you need a "math something"?

    What a bout the rest of my notes?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Dec 2007
    Posts
    107
    Thanks
    0
    Thanked 3 Times in 3 Posts
    If u have put the text box in any form then try to check the form name and the formname used in the javascript code.

    Generally for this the text bx should be kept inside a form.
    Programming & Webmaster Forum - AdSense Revenue Sharing forum

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,141
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Kor View Post
    Maybe I was vague when said "Math methods", while I should have been said "math anything, mainly math operators like + and -".
    <script type = "text/javascript">
    x = 2.3456789;
    y = x.toFixed(2);
    alert (y + " " + typeof y); // 2.35 string
    z = y-1;
    alert (z + " " + typeof z); // 1.35 number
    </script>

    In fact only + causes problems, because of course 2+2 as a string concatenates to 22.

    I have no quarrel with your other points. My post was intended to help julianflynn to get his script to work, although it contains quite a lot of other errors which need correcting, as you say.

  • #9
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, I got rid of all the "document.write" and changed them to
    "document.txt2dollar.value = twoDollarCoins"
    There are probably as you all say a lot of errors with naming and syntax but the program seems to work ok now. Thanks for the number of responses. I've gone through and fixed up as much as possible. Thank you so much I have been stuck for days.
    I'm a newbie but i'm getting there slowly.
    Thanks again to all who replied.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •