Advanced Search

Results 1 to 4 of 4

Thread: i need help...how to add 2 number...javascript

  1. #1
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i need help...how to add 2 number...javascript

    for example,

    <html>
    <head>
    <script language="JavaScript">
    function apple()
    {
    a=2;
    b=2;
    c=a+b;

    document.write("The answer is ", c);

    }
    </script>
    </head>
    <body>
    <h1> Calculate </h1>
    <form name="form1">
    <input type="button" value="Calculate" onClick="apple()">

    </form>
    </body>
    </html>

    The frustrating part is the answer is 22 instead of 4. Please help me. I am so frustrated!!!
    Last edited by doctor javascript; 11-15-2006 at 09:37 AM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script language="JavaScript">
    language is deprecated; type is required.
    Code:
    a=2;
    You omitted the var keyword. If you intend to create globals, please create them in the global scope.
    Code:
    document.write("The answer is ", c);
    You can't document.write() after the page has loaded without destroying the current page. Also, document.write() accepts only one argument.
    Code:
      <script type="text/javascript">
        function apple() {
          var a = 2,
            b = 2,
            c = a + b;
          document.getElementById("apple_op").firstChild.nodeValue = "The answer is " + c;
          return false;
        }
      </script>
    </head>
    <body>
      <h1>Calculate</h1>
      <p id="apple_op"> </p>
      <form action="" onsubmit="return apple();">
        <input type="submit" value="Calculate">
      </form>
    </body>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    document.getElementById("apple_op").firstChild.nodeValue = "The answer is " + c;

    ...

    <p id="apple_op"> </p>
    You cannot assume that the text node will exist, and indeed in MSIE it does not, causing an error.

    Mike

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oops, indeed so, my error.
    Code:
      <script type="text/javascript">
        function apple() {
          var a = 2,
            b = 2,
            c = a + b,
            op = document.getElementById("apple_op");
          if(op.firstChild)
            op.firstChild.nodeValue = "The answer is " + c;
          else
            op.appendChild(document.createTextNode("The answer is " + c));
          return false;
        }
      </script>
    </head>
    <body>
      <h1>Calculate</h1>
      <p id="apple_op"> </p>
      <form action="" onsubmit="return apple();">
        <input type="submit" value="Calculate">
      </form>
    </body>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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