Input Arrays

N

Nicko

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.
 
W

web.dev

Nicko said:
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. :)
 
N

Nigel Molesworth

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()
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Fri, 19 Aug 2005 17:50:25, seen in Nigel
Molesworth said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top