473,396 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Input Arrays

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.
Aug 19 '05 #1
5 3127

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. :)

Aug 19 '05 #2
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
Aug 19 '05 #3
JRS: In article <2b********************************@4ax.com>, dated
Fri, 19 Aug 2005 17:50:25, seen in news:comp.lang.javascript, Nigel
Molesworth <re***@thegroup.com> 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.
Aug 19 '05 #4
Dr John Stockton wrote:
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.


Thanks, I'll look that up - not sure I exactly know what a unary
mathematical function is or why parseInt should have a second parameter
but it's good to have something new to learn :)

Thanks again to everyone who replied!

Nicko.
Aug 20 '05 #5
Nicko wrote:
[...]
Thanks, I'll look that up - not sure I exactly know what a unary
mathematical function is or why parseInt should have a second parameter
but it's good to have something new to learn :)


<URL:http://www.jibbering.com/faq/#FAQ4_12>
[...]

--
Rob
Aug 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
1
by: jose luis fernandez diaz | last post by:
Hi, In the chapter 4 'Collections and Records' of the 'PL/SQL User's Guide and Reference Release 8.1.6' book there is the next paragrap: "For example, PL/SQL supports implicit (automatic)...
6
by: Peter Krikelis | last post by:
Hi All, I am having a problem setting up input mode for serial communications. (Sorry about the long code post). The following code is what I use to set up my comm port.
3
by: Prof.Stanley | last post by:
Hello all, please i am trying to write a program that recieves a string from user and counts double vowels.I have a problem particularly in the way to input the string,below is my code please will...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
5
by: joestevens232 | last post by:
Ok Im trying to use a struct to take in input from a file "grades.txt" which looks like: Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 96 100 98 92 88 96 92 86 92 94 100...
116
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however,...
11
by: arnuld | last post by:
C takes input character by character. I did not find any Standard Library function that can take a word as input. So I want to write one of my own to be used with "Self Referential Structures" of...
1
by: arnuld | last post by:
On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote: An abstract overview of my own program by Ben had helped me focus on the design issue first. I came to know that whole problem went...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.