Note that there are some explanatory texts on larger screens.

plurals
  1. POCart coding help
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>This is my javascript file with the Javascript embedded in it. I have sourced it correctly in my HTML. </p> <pre><code>&lt;SCRIPT type="text/javascript"&gt; 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='&lt;form&gt;&lt;FIELDSET&gt;&lt;LEGEND&gt;'+product+'&lt;/LEGEND&gt;'; pf+='&lt;img src="../images/'+product+'.jpg" alt="'+product+'" &gt;&lt;p&gt;price '+M+''+price+'&lt;/p&gt; &lt;b&gt;Qty&lt;/b&gt;&lt;SELECT&gt;'; for(i=0;i&lt;6;i++){pf+='&lt;option value="'+i+'"&gt;'+i+'&lt;/option&gt;'} pf+='&lt;/SELECT&gt;'; pf+='&lt;input type="button" value="Add to cart" onclick="cart()" /&gt;&lt;/FIELDSET&gt;&lt;/form&gt;'; return pf } for(j=0;j&lt;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&lt;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('&lt;br&gt;')+'&lt;h3&gt;Total '+tot+'&lt;/h3&gt;'; } &lt;/SCRIPT&gt; </code></pre> <p>Does this make sense? I'm not sure If I've explained myself correctly. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload