gemzilla
09-03-2012, 03:17 PM
Hi,
I am working on a form, which when you check a box with a given value will display the number. This part works.
However there are multiple check boxes and I would like it so if 2 boxes or more are checked it will add the two values up and display it. Currently all it does is display the highest value in a selection.
At the moment this is my Java Script Code.
var bathroom_prices = new Array();
bathroom_prices["wc"]=100;
bathroom_prices["washbasin"]=150;
bathroom_prices["bath"]=200;
bathroom_prices["shower"]=250;
bathroom_prices["bidet"]=300;
bathroom_prices["tiling"]=350;
bathroom_prices["disabled"]=50;
bathroom_prices["bathroom"]=-50;
function getbathroomprice()
{
var bathroomprice=0;
var theForm = document.forms["bathroom"];
var stuff = theForm.elements["stuff"];
for(var i = 0; i < stuff.length; i++)
{
if(stuff[i].checked)
{
bathroomprice = bathroom_prices[stuff[i].value];
break;
}
}
return bathroomprice;
}
function calculateTotal()
{
var totalbathroomprice = getbathroomprice();
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Total Price For the bathroom £"+totalbathroomprice;
}
function hideTotal()
{
var divobj = document.getElementById('totalPrice');
divobj.style.display='none';
}
I am working on a form, which when you check a box with a given value will display the number. This part works.
However there are multiple check boxes and I would like it so if 2 boxes or more are checked it will add the two values up and display it. Currently all it does is display the highest value in a selection.
At the moment this is my Java Script Code.
var bathroom_prices = new Array();
bathroom_prices["wc"]=100;
bathroom_prices["washbasin"]=150;
bathroom_prices["bath"]=200;
bathroom_prices["shower"]=250;
bathroom_prices["bidet"]=300;
bathroom_prices["tiling"]=350;
bathroom_prices["disabled"]=50;
bathroom_prices["bathroom"]=-50;
function getbathroomprice()
{
var bathroomprice=0;
var theForm = document.forms["bathroom"];
var stuff = theForm.elements["stuff"];
for(var i = 0; i < stuff.length; i++)
{
if(stuff[i].checked)
{
bathroomprice = bathroom_prices[stuff[i].value];
break;
}
}
return bathroomprice;
}
function calculateTotal()
{
var totalbathroomprice = getbathroomprice();
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Total Price For the bathroom £"+totalbathroomprice;
}
function hideTotal()
{
var divobj = document.getElementById('totalPrice');
divobj.style.display='none';
}