SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Mar 2004
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding numbers in a form with JavaScript

    Hello,

    Please could you help me with some JS? I only know my way around HTML/JS and I'm stuck here.

    I would like the user to enter two numbers. The fist number is multiplied by 25, which works.

    The second number is supposed to be added to the first result. However, as the second result I only get the second number added in writing after the first result, but not added mathematically.

    How do I need to change the code below?

    Many thanks!


    Code:
    <input type="text" id="Editbox1" name="quantity1" value="" wrap
    onKeyUp="
    val = this.value; 
    this.form.amount1.value=25*this.value;
    ">
    <input type="text" id="Editbox2" name="amount1" value="" onFocus="this.blur" readonly >
    
    <br>
    
    <input type="text" id="Editbox3" name="quantity1" value="" wrap
    onKeyUp="
    val = this.value; 
    this.form.amount.value=(this.form.amount1.value+this.value);
    ">
    
    <input type="text" id="Editbox4" name="amount" value="" onFocus="this.blur" readonly >

  2. #2
    SitePoint Addict dnordstrom's Avatar
    Join Date
    Jul 2006
    Location
    Amsterdam
    Posts
    337
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    If you want an integer (no decimals), you can use parseInt(number), so for example:

    Code JavaScript:
    parseInt(this.form.amount1.value) + parseInt(this.value)

    If you want decimals, there's parseFloat() etc, but floating point calculations in JS aren't much fun.
    Daniel Nordstrom. of. Nintera(ctive)
    -- Featured post: Part 2. Writing NI.JS JavaScript
    ----- Follow me on Twitter. Got project? Contact me.
    -------- SitePoint: Community Guidelines Be A Great Member

  3. #3
    SitePoint Addict
    Join Date
    Mar 2004
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks dnordstrom, very helpful.

    But I seems I'm not done with it need more assistance

    Could you please have a look here: http://www.fightforsight.org.uk/carrots-regfee

    Of course, the "Total to play" field doesn't update as it should. It only updates properly once the user enters something into the donation field (plus if the '0' isn't changed it displays "NaN").

    How would I need to change the code for the Total field to update straight away, and so that a user can just tab through without haveing to overtype and change the '0' for donation?

    Also, is there an easy way to make the first field "Number of participants..." a drop-down? Users should only really give a number from 1 to 6 here...

    Sorry is I'm asking a lot here, and thanks for any help!

  4. #4
    SitePoint Addict
    Join Date
    Mar 2004
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've worked it out.


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
  •