Input Arrays

Discussion in 'Javascript' started by Nicko, Aug 19, 2005.

  1. Nicko

    Nicko Guest

    Hello all,

    I'm not sure if this has been posted about recently but I'm very curious
    regarding a problem I came across today that I couldn't solve without
    PHP. I'd like to use JavaScript because it's for an extranet application
    (using IE6) that requires JavaScript anyway, so why not use it?

    Anyway, I have a few inputs in an array like so:

    <input name="inputArray[]" value="3">
    <input name="inputArray[]" value="5">
    <input name="inputArray[]" value="4">

    Basically, I'd like to use JavaScript to add up all these values and get
    the result (in this case, "12"). Is this possible? I tried a few
    different ways but none of them worked. Do these arrays work for the id
    attribute also (eg. id="inputArray[]")? If so, could I use a
    getElementById("inputArray") call or something?

    Thanks in advance,

    Nicko.
     
    Nicko, Aug 19, 2005
    #1
    1. Advertising

  2. Nicko

    web.dev Guest

    Nicko wrote:
    > Hello all,
    >
    > I'm not sure if this has been posted about recently but I'm very curious
    > regarding a problem I came across today that I couldn't solve without
    > PHP. I'd like to use JavaScript because it's for an extranet application
    > (using IE6) that requires JavaScript anyway, so why not use it?
    >
    > Anyway, I have a few inputs in an array like so:
    >
    > <input name="inputArray[]" value="3">
    > <input name="inputArray[]" value="5">
    > <input name="inputArray[]" value="4">


    Hi Nicko,

    First of all, you should know that these are not really arrays. It is
    simply a name identifier for that particular object.

    > Basically, I'd like to use JavaScript to add up all these values and get
    > the result (in this case, "12"). Is this possible? I tried a few
    > different ways but none of them worked. Do these arrays work for the id
    > attribute also (eg. id="inputArray[]")? If so, could I use a
    > getElementById("inputArray") call or something?
    >
    > Thanks in advance,
    >
    > Nicko.


    To do this work in the easiest way without rewriting your code, you can
    grab your input as a collection and then sum them up like the
    following:

    var arr = document.getElementsByName("inputArray[]");
    var arr_length = arr.length;
    var sum = 0;

    for(var i = 0; i < arr_length; ++i)
    {
    sum += parseInt(arr.value);
    }

    Hope this helps. :)
     
    web.dev, Aug 19, 2005
    #2
    1. Advertising

  3. On Fri, 19 Aug 2005 16:06:52 GMT, Nicko wrote:

    >I'd like to use JavaScript to add up all these values and get
    >the result (in this case, "12")


    Did you get "354"?

    If so, you need to convert to a number with parseFloat()

    --
    Nigel M
     
    Nigel Molesworth, Aug 19, 2005
    #3
  4. JRS: In article <>, dated
    Fri, 19 Aug 2005 17:50:25, seen in news:comp.lang.javascript, Nigel
    Molesworth <> posted :
    >On Fri, 19 Aug 2005 16:06:52 GMT, Nicko wrote:
    >
    >>I'd like to use JavaScript to add up all these values and get
    >>the result (in this case, "12")

    >
    >Did you get "354"?
    >
    >If so, you need to convert to a number with parseFloat()


    Neither parseInt nor parseFloat is *needed*; read the newsgroup FAQ.
    If something like
    sum += arr.value
    concatenates strings, just include a unary + :
    sum += +arr.value

    BTW, parseInt should always be given a second parameter unless the
    possible effect of not doing so is understood.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <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 John Stockton, Aug 19, 2005
    #4
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,683
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,267
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    571
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    387
  5. Philipp
    Replies:
    21
    Views:
    1,139
    Philipp
    Jan 20, 2009
Loading...

Share This Page