I've made a simple Javascript code for a shopping basket but now I have realised that I have made a error with making it and don't know how to fix it. What I have is a HTML file with the Javascript, but in the Javascript I have included the images source and fields that would normally only be in the HTML file but What I am trying to do now is make 2 files one a .HTML file and another .JS file, but what I want is only one button that adds to cart in the HTML file.
At the moment its has a button next to each item and then a button at the bottom. I need to get rid of the buttons next the item but I'm confused on how to do that, also I need the images sourced from the HTML file as well as the drop down boxes, but this is also in the javascript which I don't want.
This is my javascript file with the Javascript embedded in it. I have sourced it correctly in my HTML.
<SCRIPT type="text/javascript">
var items=['Xbox~149.99','StuffedGizmo~19.98','GadgetyGoop~9.97'];
var M='�'; var product=[]; var price=[]; var stuff='';
function wpf(product,price){var pf='<form><FIELDSET><LEGEND>'+product+'</LEGEND>';
pf+='<img src="../images/'+product+'.jpg" alt="'+product+'" ><p>price '+M+''+price+'</p> <b>Qty</b><SELECT>';
for(i=0;i<6;i++){pf+='<option value="'+i+'">'+i+'</option>'} pf+='</SELECT>';
pf+='<input type="button" value="Add to cart" onclick="cart()" /></FIELDSET></form>';
return pf
}
for(j=0;j<items.length;j++){
product[j]=items[j].substring(0,items[j].indexOf('~'));
price[j]=items[j].substring(items[j].indexOf('~')+1,items[j].length);
stuff+=''+wpf(product[j],price[j])+'';
}
document.getElementById('products').innerHTML=stuff;
function cart(){ var order=[]; var tot=0
for(o=0,k=0;o<document.forms.length;o++){
if(document.forms[o].elements[1].value!=0){
qnty=document.forms[o].elements[1].value;
order[k]=''+product[o]+'_'+qnty+'*'+price[o]+'';
tot+=qnty*price[o];k++
}
}
document.getElementById('inCart').innerHTML=order.join('<br>')+'<h3>Total '+tot+'</h3>';
}
</SCRIPT>
Does this make sense? I'm not sure If I've explained myself correctly.