473,396 Members | 2,029 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.

checkboxes add to a running total (in a form)???

hello people,

i'm just learning javascript, could someone point me in the direction
of an example of the following, or give me some clues as to how it
might be done:

what i would like to do is make a self totalling version of this page
http://www.ayrshirehousing.org.uk/ap...lications.html


so that basically as a person checked or unchecked boxes, a
corresponding value would be added/taken away from a total score at
the bottom of the form.

(also one other question, how do you set it up so that a person can
only check one box out of say 3 possible options? so that if they
check another box, the last one they selected is unchecked)

many thanks,

james g.
Jul 20 '05 #1
4 11774
James Greig wrote on 22 Nov 2003:
hello people,

i'm just learning javascript, could someone point me in the
direction of an example of the following, or give me some clues
as to how it might be done:

what i would like to do is make a self totalling version of this
page
http://www.ayrshirehousing.org.uk/ap...lications.html
so that basically as a person checked or unchecked boxes, a
corresponding value would be added/taken away from a total score
at the bottom of the form.


This is what I would do:

Create a form using check-boxes and radio buttons, assigning the
number of points as the 'value' for each control.

Create a JavaScript function that loops through all the form's
controls (see Form.elements collection) adding up the values then
displaying it in a text box at the bottom of the page.

Place 'onclick' events on each check-box and radio button calling
that function.
(also one other question, how do you set it up so that a person
can only check one box out of say 3 possible options? so that if
they check another box, the last one they selected is unchecked)


Use a radio button instead. By design, only one option can be
selected at a time*. Check boxes should only be used when the user
can select any of the available options.

I hope that gives you somewhere to start from.

Mike

* If the group might not apply - that is, if a circumstance is true,
they have one of three options, if not, they don't select any of them
- you could place a controlling check box that, if not checked,
causes the radio button group to be disabled or ignored.

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #2
qu********@hotmail.com (James Greig) writes:
i'm just learning javascript, could someone point me in the direction
of an example of the following, or give me some clues as to how it
might be done:

what i would like to do is make a self totalling version of this page
http://www.ayrshirehousing.org.uk/ap...lications.html
so that basically as a person checked or unchecked boxes, a
corresponding value would be added/taken away from a total score at
the bottom of the form.


I think I would just compute the total from scratch each time
something changes.

What I would do was to give each checkbox or radiobutton a value
attribute that contained the value it adds to the total, and then
run through the form like:

function calculate() {
var elems = document.forms['formID'].elements;
var total = 0;
for(var i=0;i<elems.length;i++) {
if (elems[i].checked) {total += +(elems[i].value);}
}
elems['total'].value = total;
}

and a form:

<form id="formID">
...
<label for="noBathOrShower">
<input type="checkbox" onclick="calculate()"
id="noBathOrShoewe" value="20">
No bath or shower (20)</label>
...
<label for="noHotRunningWater">
<input type="checkbox" onclick="calculate()"
id="noHotRunningWAter" value="20">
No hot running water (20)</label>
...
...
<input type="text" name="total">
</form>

You will have to think some more about cases like "Each additional
bedroom extra ....10". Either use a select element or a text input,
and change the calculate function appropriately.
(also one other question, how do you set it up so that a person can
only check one box out of say 3 possible options? so that if they
check another box, the last one they selected is unchecked)


That sounds like radiobuttons, not checkboxes. If given the same
control name, they automatically uncheck when another in the group
is checked.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
thanks you both for the advice.

i have started building the form as suggested using checkboxes, and have
now started adding radio boxes where necessary.

but i have hit a problem, each set of radio boxes has to have a unique
name, but the script i'm using to add up the total assumes that all the
radioboxes/checkboxes have the same name (Conditions).

the totalling script i'm using is:

<script language="JAVASCRIPT">
var total = 0;

function calculatetotals()
{
total = 0;
form = document.housingapplication;
for (i=0; i< form.Conditions.length; i++) {
if (form.Conditions[i].checked) {
total += parseInt(form.Conditions[i].value);
}
}
form.total.value = total;
}
</script>

am not sure how to modify this so that it adds up all of the numbers, ie
Condition1 + Condition2 + Condition3 + Condition4....

any ideas?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
james greig <qu********@hotmail.com> writes:
thanks you both for the advice.

i have started building the form as suggested using checkboxes, and have
now started adding radio boxes where necessary.

but i have hit a problem, each set of radio boxes has to have a unique
name, but the script i'm using to add up the total assumes that all the
radioboxes/checkboxes have the same name (Conditions).
Bad idea. Give them names as appropriate, and then give them all
id's starting with "Condition". Then run through all the form controls
and only add the ones where the id starts with "Condition".
the totalling script i'm using is:

<script language="JAVASCRIPT">
In HTML 4, the type attribute is required, and it is always sufficient.
<script type="text/javascript">
var total = 0;
Move this line into calcualtetotals.
function calculatetotals()
{
total = 0;
i.e.,
var total = 0;
The "var" makes this variable local to the function, so it doesn't
clutter the global object.
form = document.housingapplication;
var form = document.forms['housingapplication'];

for (i=0; i< form.Conditions.length; i++) {
for (i=0; i < form.elements.length; i++) {
if (form.Conditions[i].checked) {
if (form.elements[i].id.indexOf("Conditions")==0 &&
form.elements[i].checked)
total += parseInt(form.Conditions[i].value);
I recommend giving parseInt a second argument of 10. That enforces
the interpretation of the first argument as a base 10 numeral.
Since you wrote all the values yourself, it's probably not necessary,
but it's a good habit.
}
}
form.total.value = total;
form.elements['total'].value = total;
}
</script>

am not sure how to modify this so that it adds up all of the numbers, ie
Condition1 + Condition2 + Condition3 + Condition4....


See above. Use the id attribute to give them different ID's, use the
name attribute to group radiobuttons.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

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

Similar topics

5
by: fwells11 | last post by:
Hi there. As you will see from my questions, I am an SQL newb. I dabble but never get to spend enough time to get proficient so base any feeedback on that basis please. This is all theoretical...
4
by: Bill Dika | last post by:
Hi I am trying to calculate a running total of a calculated textbox (tbAtStandard) in GroupFooter1 for placement in a textbox (tbTotalAtStandard) on my report in Groupfooter0. The problem...
16
by: Gandalf186 | last post by:
I need to create a query that produces running totals for every group within my table for example i wish to see: - Group A 1 5 9 15 Group B
2
by: Jana | last post by:
Using Access 97. Background: I have a main report called rptTrustHeader with a subreport rptTrustDetails in the Details section of the main report. The main report is grouped by MasterClientID. ...
5
by: masterej | last post by:
Developers, Is there any way to disable all checkboxes on a form? I have a form with 160 checkboxes and I want to be able to disable all of them. Is there a way I can do something like this: ...
12
jaccess
by: jaccess | last post by:
Hello all, I am trying to create a running total based on a specific date range that is to be entered into a form. I currently have the form set up with 2 text boxes (date1 and date2) which are...
6
by: Stuart Shay | last post by:
Hello All: I have a array which contains the totals for each month and from this array I want to get a running total for each month decimal month = new decimal; month = 254; (Jan) month =...
1
by: Bruce | last post by:
I had a form with a running total working until I was asked to add some checkboxes. Here is what I have: http://www.bearzilla.net/test/Untitled-1.html The first section works, but I can't get...
4
by: Jinzuku | last post by:
Hi, I'm trying to create a form that has a running total of the amount an organisation has been invoiced. 1 table (Database) consists of everything I've invoice out. The fields this table has is...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.