Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Input Arrays

Reply
Thread Tools

Input Arrays

 
 
Nicko
Guest
Posts: n/a
 
      08-19-2005
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.
 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      08-19-2005

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[i].value);
}

Hope this helps.

 
Reply With Quote
 
 
 
 
Nigel Molesworth
Guest
Posts: n/a
 
      08-19-2005
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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      08-19-2005
JRS: In article <(E-Mail Removed)>, dated
Fri, 19 Aug 2005 17:50:25, seen in news:comp.lang.javascript, Nigel
Molesworth <(E-Mail Removed)> 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[i].value
concatenates strings, just include a unary + :
sum += +arr[i].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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments