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 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple add 2 numbers problem...

    what am I missing...


    <HTML>
    <HEAD>
    <TITLE>Help Me</TITLE>
    <SCRIPT LANGUAGE="JavaScript 1.2">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    function addNums() {
    var num1 = document.testForm.txtNum1.value;
    var num2 = document.testForm.txtNum2.value;
    var total = num1 + num2;
    document.testForm.txtTotal.value = total;
    // STOP HIDING -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="testForm">
    <P>1st Number:
    <INPUT TYPE="text" NAME="txtNum1" SIZE=5></P>
    <P>2nd Number:
    <INPUT TYPE="text" NAME="txtNum2" SIZE=5></P>
    <P>Result:
    <INPUT TYPE="text" NAME="txtTotal" SIZE=10></P>
    <P>
    <INPUT TYPE="button" VALUE="Add" onClick="addNums();">
    <INPUT TYPE="reset"></P>
    </FORM>
    </BODY>
    </HTML>

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The value of the form fields is a string, not a number. You are concatenating strings and not adding numbers. Use parseInt or parseFloat to get the numbers from the strings.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats not all... I'm getting weird errors

    it's saying object missing on this line

    <INPUT TYPE="button" NAME="button_Add" VALUE="Add" onClick="addNums();">

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Asside from whats allready been pointed out, Use <script type="text/javascript">...your also missing an ending curly brace on your function:
    Code:
    <SCRIPT type="text/javascript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    function addNums() {
    var num1 = document.testForm.txtNum1.value;
    var num2 = document.testForm.txtNum2.value;
    var total = num1 + num2;
    document.testForm.txtTotal.value = total;
    }
    // STOP HIDING -->
    </SCRIPT>
    Last edited by Brandoe85; 03-08-2005 at 02:48 PM.

  • #5
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still saying object expected for this line

    <INPUT TYPE="button" VALUE="Add" onClick="addNums();">

    ???

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Location
    In front of this computer. (Where else?)
    Posts
    442
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Did you take liorean's suggestion? Use parseInt or parseFloat to get the numbers from the floats. If that's still giving you an error, then post your new code. Maybe you are not using them properly
    See here for a reference.
    Tomorrow is the first day of the rest of your life... What have you done today?
    Tutorials
    Web: w3schools, htmldog General tips: Hardware, Networking

  • #7
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    run the program.. click the add # after entering 2 numbers

    <HTML>
    <HEAD>
    <TITLE>Debugging Problem</TITLE>
    <SCRIPT LANGUAGE="JavaScript 1.2">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    function addNums() {
    var num1 = parseFloat(document.testForm.txtNum1.value);
    var num2 = parseFloat(document.testForm.txtNum2.value);
    var total = num1 + num2;
    document.testForm.txtTotal.value = total;
    }
    // STOP HIDING -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="testForm">
    <P>1st Number:
    <INPUT TYPE="text" NAME="txtNum1" SIZE=5></P>
    <P>2nd Number:
    <INPUT TYPE="text" NAME="txtNum2" SIZE=5></P>
    <P>Result:
    <INPUT TYPE="text" NAME="txtTotal" SIZE=10></P>
    <P>
    <INPUT TYPE="button" VALUE="Add" onClick="addNums();">
    <INPUT TYPE="reset"></P>
    </FORM>
    </BODY>
    </HTML>

  • #8
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Did you change this line:
    <SCRIPT LANGUAGE="JavaScript 1.2">
    to:
    <script type="text/javascript">

    As I pointed out in my previous post...


  •  

    Posting Permissions

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