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

adding up a total

Here is a sample form that I just made up. I would like a somekind of
script that when each button is checked or unchecked to dynamicly add or
subtract the total. If anyone can help, thanks alot.

<html>

<head>

<title>Testing addition of javascript</title>
</head>

<body>

<form method="POST" action="">
<p><input type="checkbox" name="C1" value="50">item 1 $50</p>
<p><input type="checkbox" name="C2" value="10">item 2 $10</p>
<p><input type="checkbox" name="C3" value="30">item 3 $30</p>
<p><input type="checkbox" name="C4" value="60">item 4 $60</p>
<p>Total $ totalhere</p>
</form>

</body>
<script type="text/javascript">

</script>
</html>
Jul 23 '05 #1
1 1783
Samir wrote:
Here is a sample form that I just made up. I would like a somekind of
script that when each button is checked or unchecked to dynamicly add or
subtract the total. If anyone can help, thanks alot.

[snip]

Add an "onchange" function to each checkbox that calls a function to
cycle through the checkboxes and add up those that are checked, e.g.:

<p><input type="checkbox" name="C1" value="50"
onchange="writeTotal(this.form,'total');">item 1 $50</p>

then write the total to the paragraph.

function writeTotal(x,y) {
var total = 0;
for (var i=0;i<x.elements.length; i++) {
if (x.elements[i].type == 'checkbox'
&& x.elements[i].checked) {
total += +x.elements[i].value;
} }
document.getElementById(y).innerHTML = total;
}

Of course this is just a minimal example, you need to add feature
detection and handle cases where the user has JavaScript disabled.

You'll need a custom reset button because the normal one will not
change the total in the paragraph:

<input type="button" value="Clear all"
onclick="this.form.reset();writeTotal(this.form,'t ot');">

Also, you need to ensure that when the page is re-loaded, the form is
re-set as some browsers will clear the check boxes but not update the
paragraph text (or vice versa) - say an 'onload' function to reset the
form.

You can add an id to the para, or put in a <span> with an id and just
write the number:

<p>Total $ <span id="total">0</span></p>

I'd put your form into a table, then use character alignment to get all
your numbers to line up.

Cheers, Fred.
Jul 23 '05 #2

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

Similar topics

1
by: Frank | last post by:
I have a large form, that has text boxes of numbers in rows and columns. I need to sum the values in the columns, and put the total at the bottom of the column. But I also need to sum the values in...
4
by: Abby Lee | last post by:
Can someone tell me what is wrong with this statement? function subtotal(){ var ttot = 1; for (var i = 1; i < 7; i++){ if(isPosInt(document.forms.total(i).value)){ ttot +=...
6
by: 6thirty | last post by:
Hi, I've created a stocktaking database using Access XP. This is indexed by two fields - part number and shelf location. I am currently inputting all the data via a form. When I have entered a...
0
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to...
2
by: Stan Horwitz | last post by:
I am working on a perl program where I extract a variable from a line of input and I want to keep a running count of that variable. I am using the substr function to extract the variable. For...
0
by: chrisbenoit06 | last post by:
Howdy...anyone able to assist me in adding the function to calculate the gross pay, and also the calculation and printing of the totals of hours and gross pay....thoughts? /* Purpose: This...
1
by: Shawn Yates | last post by:
It has been a while since I have done anything on MS Access and I seem to be a bit rusty. I hope someone could help me solve my issue. I have a form that has multiple combo boxes on it. Each box...
1
by: methexandude | last post by:
Hi, I'm populating a datagrid with values and adding one of the columns to get a total. It adds up correctly but rounds off the values. For example if the total is 2.56789 it makes it three as a...
5
by: JohnSmith70 | last post by:
I'm trying to add two values. When adding money it very simple when its 50p+20p=70p. But if I add £1 then I face a problem because 70+1=71 wrong. So I tried to enter £1 as 100p so I have 170p. But...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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.