Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Simple Adding Calculator - Javascript

  1. #11
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question

    Hello again Twey. I´ve just read through the code you gave me again and I notice that there is no "parseInt()" function written. As mentioned in my original problem, I need to include this. If you could be so kind as to give me a hint on where I might use it? If I don´t hear back, I will understand that you´re too busy. Thank you again.

    Quote Originally Posted by Twey View Post
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>The Adding Calculator</title>
      </head>
      <body>
        <h1>The Adding Calculator</h1>
        <div>
          <form action="" onsubmit="this.elements.total.value = +this.elements.n1.value + (+this.elements.n2.value);">
            <label>
              First number:
              <input type="text" name="n1">
            </label>
            <label>
              Second number:
              <input type="text" name="n2">
            </label>
            <label>
              Total:
              <input type="text" name="total">
            </label>
            <input type="submit" value="Calculate">
          </form>
        </div>
      </body>
    </html>

  2. #12
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>The Adding Calculator</title>
      </head>
      <body>
        <h1>The Adding Calculator</h1>
        <div>
          <form action="" onsubmit="this.elements.total.value = +this.elements.n1.parseInt(value) + (+this.elements.n2.parseInt(value));">
            <label>
              First number:
              <input type="text" name="n1">
            </label>
            <label>
              Second number:
              <input type="text" name="n2">
            </label>
            <label>
              Total:
              <input type="text" name="total">
            </label>
            <input type="submit" value="Calculate">
          </form>
        </div>
      </body>
    </html>

  3. #13
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    I did used to search everything on google - that&#180;s how I came across this site, decided it was the best resource I could find and stuck with it.
    Completely understandable.

    However, realize the two sides to web design/coding:
    1. Creative
    2. Technical

    Every day, I look up some function. There are just way too many to remember, with way too many properties. No one can keep up. (Well, perhaps Twey can ... hmm? Ha. Or John. Anyway...)

    The technical stuff is easy, once you get to a point where you can understand it (should happen soon for you).

    The next part comes with experience. What works how, and what's the best approach to troubleshoot, etc. That's where we can be of help, frequently.
    Standards, efficiency, etc., are something that must be learned. Not very easy to google at times, as well.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

    Hello again Twey. I&#180;ve just read through the code you gave me again and I notice that there is no "parseInt()" function written. As mentioned in my original problem, I need to include this.
    If it makes you happy, you can think of the (+n) form as being equivalent to parseInt(n, 10). They serve the same function in this case, but +n is quicker to write and more efficient to run.
    +this.elements.n1.parseInt(value)
    No. That would be parseInt(this.elements.n1.value, 10).
    Every day, I look up some function. There are just way too many to remember, with way too many properties. No one can keep up. (Well, perhaps Twey can ... hmm? Ha. Or John. Anyway...)
    I don't tend to use references very often any more. When I need to remind myself of the syntax or effects of a particular function (which I do still need to do for some of the array functions that I don't use as often as I ought, like .slice() and .splice()), I usually just try it and see in my command-line Javascript shell. I still make moderate use of CSS references when designing any non-trivial layout, since it's harder to test. The PHP interactive interpreter is a lot less friendly, too, and PHP functions have stupidly irregular names and parameter orders that I can never remember, so I refer to the php.net API references heavily.

    Just to throw a spanner in your theory, I pick up both human and computer languages pretty well, but I'm hopeless at maths.
    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!

  5. #15
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    I'm good with language and with math.
    I'm also very creative. Though I wouldn't say I excel at figurative meanings in literature/poetry
    I also am not very musical (I don't play an instrument and I don't really listen to music, but I do like some music).


    As for PHP, yeah. That's actually what I meant mostly. Used to html by now.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #16
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Talking

    Well, thank you again. It´s really interesting to hear the experiences of the experienced!

    And the spanner, well, that just shows me that if we´re both terrible at maths, there must be some hope for me after all when it comes to Javascript.

    I´ve not got to the part of my course yet that deals with CSS or PHP... heaven help me!

    Have a lovely evening everyone!

  7. #17
    Join Date
    Jan 2006
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hah, thanks for the code - change it up a little for my project

    http://ccms.mediared.net/a/cc/cc.php?n1=&n2=&total=

  8. #18
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Code:
    <form action="" onSubmit="this.elements.total.value = +this.elements.n1.value + (+this.elements.n2.value); return false;">
    That will make the page not submit, which is a bit weird.

    You SHOULD have a backup server side solution, so that it works if the user doesn't have javascript installed.

    You could do that easily with PHP like this:
    <input type="text" value="<?php echo $_POST['field1']+$_POST['field2']; ?>">


    EDIT: Hmm.. nevermind. Looks like you do have this setup already. Try return false, and that should make it all work.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    Default

    Whoops, yes, I forgot to return false. Silly me.
    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
  •