Connecting Tech Pros Worldwide Help | Site Map

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

James Greig
Guest
 
Posts: n/a
#1: Jul 20 '05
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[color=blue]
> http://www.ayrshirehousing.org.uk/ap...lications.html[/color]

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.
Michael Winter
Guest
 
Posts: n/a
#2: Jul 20 '05

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


James Greig wrote on 22 Nov 2003:
[color=blue]
> 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[color=green]
>> http://www.ayrshirehousing.org.uk/ap...lications.html[/color]
>
> 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.[/color]

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.
[color=blue]
> (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)[/color]

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.Winter@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#3: Jul 20 '05

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


quietjames@hotmail.com (James Greig) writes:
[color=blue]
> 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[color=green]
>> http://www.ayrshirehousing.org.uk/ap...lications.html[/color]
>
> 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.[/color]

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.
[color=blue]
> (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)[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
james greig
Guest
 
Posts: n/a
#4: Jul 20 '05

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


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!
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#5: Jul 20 '05

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


james greig <quietjames@hotmail.com> writes:
[color=blue]
> 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).[/color]

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".
[color=blue]
> the totalling script i'm using is:
>
> <script language="JAVASCRIPT">[/color]

In HTML 4, the type attribute is required, and it is always sufficient.
<script type="text/javascript">
[color=blue]
> var total = 0;[/color]

Move this line into calcualtetotals.
[color=blue]
> function calculatetotals()
> {
> total = 0;[/color]

i.e.,
var total = 0;
The "var" makes this variable local to the function, so it doesn't
clutter the global object.
[color=blue]
> form = document.housingapplication;[/color]

var form = document.forms['housingapplication'];

[color=blue]
> for (i=0; i< form.Conditions.length; i++) {[/color]

for (i=0; i < form.elements.length; i++) {
[color=blue]
> if (form.Conditions[i].checked) {[/color]

if (form.elements[i].id.indexOf("Conditions")==0 &&
form.elements[i].checked)
[color=blue]
> total += parseInt(form.Conditions[i].value);[/color]

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.
[color=blue]
> }
> }
> form.total.value = total;[/color]

form.elements['total'].value = total;
[color=blue]
> }
> </script>
>
> am not sure how to modify this so that it adds up all of the numbers, ie
> Condition1 + Condition2 + Condition3 + Condition4....[/color]

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

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread