This is my first shot at writing a JavaScript to calculate a shopping cart. Naturally, it doesn’t work. I was hoping someone out there would be able to offer some suggestions as to how to fix it.
Thanks in advance for your help!!
/* I built this script based on the guidance and examples I found
here:
http://www.javascript-coder.com/javascript-form/javascript-calcul
ator-script.phtml
*/
<script>
var theForm = document.forms["order"];
var bakery_prices= new Array();
bakery_prices["None"]=0;
bakery_prices["sourdough"]=5;
bakery_prices["bread"]=3;
var mns_prices= new Array();
mns_prices["None"]=0;
mns_prices["filet"]=5;
mns_prices["fish"]=5;
var produce_prices= new Array();
produce_prices{["None"]=0;
produce_prices["asparagus"]=3;
produce_prices["pineapple"]=2;
var cnd_prices= new Array();
cnd_prices["None"]=0;
cnd_prices["cheddar"]=3;
cnd_prices["milk"]=4;
var wine_prices= new Array();
wine_prices["None"]=0;
wine_prices["chardonnay"]=25;
wine_prices["merlot"]=20;
function getbakeryPrice()
{
var bakeryPrice=0;
var theForm = document.forms["order"];
var selectedbakery = theForm.elements["bakery"];
bakeryPrice = bakery_prices[selectedbakery.value];
return bakeryPrice;
}
function getmnsPrice()
{
var mnsPrice=0;
var theForm = document.forms["order"];
var selectedmns = theForm.elements["mns"];
mnsPrice = mns_prices[selectedmns.value];
return mnsPrice;
}
function getproducePrice()
{
var producePrice=0;
var theForm = document.forms["order"];
var selectedproduce = theForm.elements["produce"];
mnsPrice = produce_prices[selectedproduce.value];
return producePrice;
}
function getcndPrice()
{
var cndPrice=0;
var theForm = document.forms["order"];
var selectedcnd = theForm.elements["cnd"];
mnsPrice = cnd_prices[selectedcnd.value];
return cndPrice;
}
function getwinePrice()
{
var winePrice=0;
var theForm = document.forms["order"];
var selectedwine = theForm.elements["wine"];
mnsPrice = wine_prices[selectedwine.value];
return winePrice;
}
function calculateTotal()
{
var totalPrice = getbakeryPrice() + getmnsPrice() +
getproducePrice() + getcndPrice() + getwinePrice();
var divobj = document.getElementById('totalPrice');
divobj.style.display="block";
divobj.innerHTML = "Your Order Total is $"+totalPrice;
}
function hideTotal()
{
var divobj = document.getElementById('totalPrice');
divobj.style.display='none';
}
</script>