Results 1 to 7 of 7

Thread: How to add in a form

  1. #1
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default How to add in a form

    Hi,

    How do you add two numbers in a form?

    I tried it and it went like 11

    When i said it to add the two numbers together.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    the + (plus) symbol in javascript means combine...

    "some"+"thing" = "something"

    therefore, you seem to be adding two strings together... the string "1" and the string "1".
    But if you were using an integer or other numerical variable, it would do the math operation.

    One way to know is if you're typing "1"+"1" vs. typing 1+1. The first will give 11, the second 2. The quotes make it be a string.

    I'm not too familiar with variable types in javascript, but just change your strings to numbers... either by not using quotes or by setting the variable type of what you're using... user input will probably default to a string, not number.

    Good luck. Hope this at least points you in the right direction.

    Since I'm not an expert at JS, some of the details might be slightly off, but I'm almost positive the general idea is right.
    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

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Let's see...

    I tried:
    Code:
    document.session.timeschanged.value = "1+1"
    It gave me 1+1 (of course!)

    And tried:

    Code:
    document.session.timeschanged.value = document.session.timeschanged.value+1
    It gave me 11

    And tried:
    I tried:
    Code:
    document.session.timeschanged.value = "1"+"1"
    It gave me 1+1 as well...

    What am I suppose to do?

    I want the user to click a button and it adds one. Like the current value is "1" and i want the user to click a button and it adds "1" so it displays "2" not "11"

    I'm not the expert in JS as well, but at least it will do if i'm only 14
    Last edited by tech_support; 05-31-2006 at 06:48 AM. Reason: A boo-boo! :)
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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

    Default

    For a start, you should never assume that forms will be available as properties of the document element. You should be using the forms and elements collections, like so:
    Code:
    document.forms['session'].elements['timechanged'].value
    Try this one:
    Code:
    document.forms['session'].elements['timechanged'].value++;
    + is both a string and a number operation, so the parser could easily be forgiven for not performing type conversion there. In fact, it's probably not supposed to. ++, the post-increment operator, however, is purely a numerical operator, so the parser should be forced to convert the string to a number.
    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. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Listen to what Twey said... that should do it.

    As for your last post... each example there is wrong.

    "1+1":
    Things in quotes are just strings... they aren't evaluated... even operators, etc... just like "dosomething()" would just be a string, not a function.

    document.session.timeschanged.value+1:
    again... your variable is, as user input, by default, a string... meaning you're adding "1" to the end of the string "1".... so... 11.

    "1"+"1":
    Yeah. Two strings.


    Just 1+1 with NO quotes would give you the right answer.
    However, that doesn't help with the user input thing.

    the something++ method should work.

    If nothing else, you could just cheat and do:
    something++; something--; and that should give you the original, but as a number. Maybe... not sure exactly how this works.
    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. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    something++; something--; and that should give you the original, but as a number.
    If it did, then there would be no need to use it
    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!

  7. #7
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Thanks for your help guys!
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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
  •