473,396 Members | 1,827 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.

Sum inputs that id starts with

hi there,

I have several forms that have a total field with the id gtotal1 gtotal2 gtotal3 ..... up to 35

I would like a Grand Total of those fields. I would like to SUM those fields.

I am looking for a code like:

If input id contains gtotal then sum that inputs value.. something like that.

I hope i was clear
Jan 8 '10 #1
5 2545
RamananKalirajan
608 512MB
Hi Kyriacoullis,
You can easily do that,

Expand|Select|Wrap|Line Numbers
  1. var totSum=0;
  2. for(var i=0;i<100;i++)
  3. {
  4.   var tempId="gtotal"+i;
  5.   try
  6.   {
  7.   if(document.getElementById(tempId).value!="")
  8.       totSum+=document.getElementById(tempId).value;
  9.   }
  10.   catch(e)
  11.   {
  12.     alert(e.message);
  13.   }
  14. }
  15. alert("Total Sum = "+totSum);
Thanks and Regards
Ramanan Kalirajan
Jan 8 '10 #2
Dormilich
8,658 Expert Mod 8TB
the best would be to give each of those fields a common class name, so that you can access the elements with document.getElementsByClassName()

EDIT: @Ramanan I think it’s not worth testing each ID, they are just not made for this purpose.
Jan 8 '10 #3
RamananKalirajan
608 512MB
@ Dormilich,
Ya I am agreeing your point but document.getElementsByClassName() is not supported in IE. It will work fine in Mozilla. But it wont be working in IE, we have write a user defined function for that. But coding with ID will be working in all the browsers.

Thanks and Regards
Ramanan Kalirajan
Jan 8 '10 #4
acoder
16,027 Expert Mod 8TB
How about taking any good version of getElementsByClassName and including it for any browsers that don't support it (by testing for its support):
Expand|Select|Wrap|Line Numbers
  1. if (!document.getElementsByClassName)
Jan 8 '10 #5
Dormilich
8,658 Expert Mod 8TB
for instance Robert Nyman’s getElementsByClassName()
Jan 8 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: WindAndWaves | last post by:
Hi Gurus I have a php page with a form in it, that is along the lines of: <form...> item with id 234 <input type=submit ... NAME=add234 ID=add234> <input type=hidden NAME=add234 ID=234>...
2
by: Chris Beach | last post by:
Hi there. I'd like to be able to group together inputs on an HTML form inside, say, a <div> or <fieldset> and then use javascript to iterate through the controls in the group and disable them....
11
by: Sven Neuberg | last post by:
Hi, I have been handed the task of updating and maintaining a web application, written in ASP and Javascript, that takes complex user inputs in HTML form and submits them to server-side ASP...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
4
by: StevensSpam | last post by:
How do I retrieve user inputs from web page into the C# program that is running behind? I am using Visual Studio to create an ASP.NET web application with code behind (aspx.cs) feature. Also, how...
18
by: shana07 | last post by:
Hi All, I do really need to consult you guys about my java assignment. Please guide me how to make an algorithm to select an input from a set of inputs? Suppose I have a program p (a), an...
7
by: shana07 | last post by:
good day all! Need to consult about input array in main. I have one program that need two inputs (a & b) in main. Please see codes below: public static void main(String args) { ...
1
by: dawidg | last post by:
I have an asp.net page with viewstate disabled. I have a control on the page that dynamically creates controls inside itself, no viewstate. The controls aren't created in Init, but in Load. With...
2
by: ramprat | last post by:
Hi Everyone, I have been going crazy trying to figure this out and I'm sure it's easy. I have a continuous form based on a table. Once someone enters a new record on the form I have code in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.