addition

Discussion in 'Javascript' started by magix, Mar 14, 2007.

  1. magix

    magix Guest

    I have following function, but how can I do addition ?
    if
    f.Field1.value =2
    f.Field3.value =3
    f.Field4.value =1

    then
    f.Total.value =231, instead of 6


    function update( ) {
    var f = document.myform
    f.Total.value = f.Field1.value + f.Field2.value + f.Field3.value
    return true;
    }

    Many thanks.
    magix, Mar 14, 2007
    #1
    1. Advertising

  2. magix

    magix Guest

    Sorry, I mean I want it 6, instead of 231

    "magix" <> wrote in message news:...
    >I have following function, but how can I do addition ?
    > if
    > f.Field1.value =2
    > f.Field3.value =3
    > f.Field4.value =1
    >
    > then
    > f.Total.value =231, instead of 6
    >
    >
    > function update( ) {
    > var f = document.myform
    > f.Total.value = f.Field1.value + f.Field2.value + f.Field3.value
    > return true;
    > }
    >
    > Many thanks.
    >
    magix, Mar 14, 2007
    #2
    1. Advertising

  3. magix

    Guest

    On Mar 14, 1:38 pm, "magix" <> wrote:
    > I have following function, but how can I do addition ?


    > function update( ) {
    > var f = document.myform
    > f.Total.value = f.Field1.value + f.Field2.value + f.Field3.value
    > return true;
    > }


    function update( ) {
    var f = document.forms['myform'] //- this part is not important
    f.Total.value = +f.Field1.value + +f.Field2.value + +f.Field3.value
    return true;

    }
    , Mar 14, 2007
    #3
  4. magix

    Tim Slattery Guest

    "magix" <> wrote:

    >I have following function, but how can I do addition ?
    >if
    > f.Field1.value =2
    > f.Field3.value =3
    > f.Field4.value =1


    parseInt(f.Field1.value) + parseInt(f.Field3.value) +
    parseInt(f.Field4.value)

    --
    Tim Slattery

    http://members.cox.net/slatteryt
    Tim Slattery, Mar 14, 2007
    #4
  5. magix

    RobG Guest

    On Mar 15, 5:38 am, "magix" <> wrote:
    > I have following function, but how can I do addition ?
    > if
    > f.Field1.value =2
    > f.Field3.value =3
    > f.Field4.value =1
    >
    > then
    > f.Total.value =231, instead of 6
    >
    > function update( ) {
    > var f = document.myform
    > f.Total.value = f.Field1.value + f.Field2.value + f.Field3.value
    > return true;
    >
    > }


    The values returned by form controls are strings, they must be
    converted to numbers or the '+' operator will cause concatenation, not
    addition:

    <URL: http://www.jibbering.com/faq/#FAQ4_21 >


    --
    Rob
    RobG, Mar 14, 2007
    #5
  6. magix

    Matt Kruse Guest

    Matt Kruse, Mar 14, 2007
    #6
  7. In comp.lang.javascript message <2hlgv2l2ggbg5cdpv9mdp4h4cukpoofada@4ax.
    com>, Wed, 14 Mar 2007 16:11:10, Tim Slattery <>
    posted:
    >"magix" <> wrote:
    >
    >>I have following function, but how can I do addition ?
    >>if
    >> f.Field1.value =2
    >> f.Field3.value =3
    >> f.Field4.value =1

    >
    >parseInt(f.Field1.value) + parseInt(f.Field3.value) +
    >parseInt(f.Field4.value)


    parseInt should only be used without a second parameter under specific
    conditions.

    For a good answer, quoting FAQ 4.21 is necessary and should be
    sufficient.

    For multiple fields, consider
    - ( -f.Field1.value -f.Field2.value -f.Field3.value )

    Or write a function FV(g) { return +g.value } and use it as
    FV(f.Field1) + FV(f.Field2) + FV(f.Field3)



    It's a good idea to read the newsgroup and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Mar 15, 2007
    #7
  8. magix

    Evertjan. Guest

    Dr J R Stockton wrote on 15 mrt 2007 in comp.lang.javascript:

    > For a good answer, quoting FAQ 4.21 is necessary and should be
    > sufficient.
    >
    > For multiple fields, consider
    > - ( -f.Field1.value -f.Field2.value -f.Field3.value )
    >
    > Or write a function FV(g) { return +g.value } and use it as
    > FV(f.Field1) + FV(f.Field2) + FV(f.Field3)


    A moe general solution:

    function sumOfFields(begin,end,frm,elem){
    var r = 0;
    for (var i=begin; i<=end; i++)
    r -= frm.elements[elem+i].value;
    return -r;
    ]

    result = sumOfFields(1,3,f,'Field')

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Mar 15, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Midnight Java Junkie

    Office Development (feature addition)

    Midnight Java Junkie, Jun 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    453
    Midnight Java Junkie
    Jun 20, 2004
  2. lezah
    Replies:
    7
    Views:
    756
    Mike Treseler
    Mar 2, 2004
  3. Julian

    Addition of one

    Julian, Oct 18, 2004, in forum: VHDL
    Replies:
    13
    Views:
    3,686
    rickman
    Oct 21, 2004
  4. Clemens Bosch

    Beginners questions for addition

    Clemens Bosch, Dec 2, 2004, in forum: VHDL
    Replies:
    0
    Views:
    548
    Clemens Bosch
    Dec 2, 2004
  5. jiten

    about addition operator

    jiten, Jun 22, 2005, in forum: VHDL
    Replies:
    3
    Views:
    4,457
Loading...

Share This Page