- Jan 2, 2006
- 10,455
- 35
- 91
Code:
//These are two checkboxes for a form.
<input type="checkbox" id="spw" name="spw" value="Single Property Website" amount="25" onchange="sum_amount()" checked />
<label for="spw"> Single Property Website: $25<br></label>
<input type="checkbox" id="panoramas" name="panoramas" value="Panoramas / Virtual Tour Effect" amount="20" onchange="sum_amount(); return checkbox();" >
<label for="panoramas">Panoramas: $20<br></label>
//If the Panoramas checkbox gets checked, the SPW checkbox automatically gets checkmarked as well.
function checkbox()
{
if(document.getElementById("panoramas").checked==true)
{
document.getElementById("spw").checked=true;
}
}
//If the SPW checkbox is checked, the total_sum is 25.
//If Panoramas is checked, total_sum is 20.
//If both are checked, total_sum is 45.
function sum_amount()
{
var total_sum=0;
if(document.getElementById("spw").checked==true)
{
total_sum+=25;
}
if(document.getElementById("panoramas").checked==true)
{
total_sum+=20;
}
}
The problem here is with total_sum and onchange and checkbox()
With both SPW and Panoramas checkmarked, the total_sum should be 45.
If I manually checkmark SPW, total_sum is 25
If I then checkmark Panoramas, total_sum is 45.
BUT IF..
Both boxes start off unchecked.
I checkmark Panoramas.
The checkbox() function will then automatically checkmark SPW.
So now both boxes are checked.
But the total_sum is 20, not 45.
If I toggle the SPW checkbox off and then back on, total_sum changes to the correct amount of 45.
Why is it that when I use the checkbox() function to automatically checkmark SPW, the onchange event does not work and total_sum is not correct?