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.'