Advertisement
here2share

HTML Basic Pizza Ordering Demo

Feb 21st, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html>
  3.  
  4. <head>
  5.     <title>HTML Basic Pizza Ordering Demo</title>
  6.  
  7. </head>
  8.  
  9. <body>
  10.  
  11. <style>
  12. fieldset {
  13.     font-family: arial, sans-serif;
  14. }
  15.  
  16. table {
  17.     border-collapse: collapse;
  18.     width: 100%;
  19.     table-layout: fixed;
  20. }
  21.  
  22. td {
  23.     border: 1px solid #999999;
  24.     text-align: left;
  25.     padding: 8px;
  26. }
  27.  
  28. tr {
  29.     background-color: #dddddd;
  30. }
  31. </style>
  32.  
  33. <body>
  34.  
  35. <form action="#" method="post" class="demoForm" id="demoForm">
  36.     <fieldset>
  37.     <legend><b>Hello Pizza World!</b></legend>
  38.    
  39.     <table id="sizes">
  40.         <tr>
  41.         <label>Choose Pizza Size:</label><br>
  42.         <td><input type="radio" name="size" id="Extra Large" size="16" value="12.75">Extra Large ($12.75)</td>
  43.         <td><input type="radio" name="size" id="Large" size="12" value="10.50">Large ($10.50)</td>
  44.         <td><input type="radio" name="size" id="Medium" size="10" value="8.25" checked>Medium ($8.25)</td>
  45.         <td><input type="radio" name="size" id="Small" size="8" value="6">Small ($6)</td>
  46.         </tr>
  47.     </table>
  48.     <br>
  49.     <table id="toppings">
  50.         <tr>
  51.         <label>Toppings:</label><br>
  52.         <td><input type="checkbox" name="extra cheese" value="1.00" /> Extra Cheese (+1.00)</td>
  53.         <td><input type="checkbox" name="beef" value=".60" /> Beef (+0.60)</td>
  54.         <td><input type="checkbox" name="chicken" value=".60" /> Chicken (+0.60)</td>
  55.         <td><input type="checkbox" name="green peppers" value=".30" /> Green Peppers (+0.30)</td>
  56.         <td><input type="checkbox" name="bacon" value=".50" /> Bacon (+0.50)</td>
  57.         </tr>
  58.         <tr>
  59.         <td><input type="checkbox" name="mushrooms" value=".40" /> Mushrooms (+0.40)</td>
  60.         <td><input type="checkbox" name="onions" value=".30" /> Onions (+0.30)</td>
  61.         <td><input type="checkbox" name="black olives" value=".40" /> Black Olives (+0.40)</td>
  62.         <td><input type="checkbox" name="sausage" value=".50" /> Sausage (+0.50)</td>
  63.         <td><input type="checkbox" name="pepperoni" value=".50" /> Pepperoni (+0.50)</td>
  64.         </tr>
  65.     </table>
  66.    
  67.     <p>
  68.         <i><font color="red">
  69.         TOTAL: $<label id="total">8.25</label></i></font>
  70.         <br><br>
  71.         SIZE: <label id="pizza">Medium</label><br><br>
  72.         ITEMS:<br><label id="items">***</label>
  73.     </p>
  74.     </fieldset>
  75.    
  76. </form>
  77.  
  78. <script>
  79.  
  80. var hl = "rgb(153, 204, 255)"
  81. var hl_x = hl + " none repeat scroll 0% 0%"
  82.  
  83. // get reference to element containing toppings checkboxes
  84. var cb = document.getElementById('toppings');
  85.  
  86. // get reference to input elements in toppings container element
  87. var tops = cb.getElementsByTagName('input');
  88.  
  89. // get reference to input elements in toppings container element
  90. var cells = cb.getElementsByTagName('td');
  91.  
  92. // assign updateChecks function to onclick property of each checkbox
  93. for (var i=0, len=tops.length; i<len; i++) {
  94.     cells[i].index = i
  95.     cells[i].addEventListener('click', function (e) {
  96.         var i = this.index;
  97.         if ( this.style.background === hl_x ) {
  98.         this.style.background = "#DDDDDD";
  99.         tops[i].checked = false;
  100.         } else {
  101.         this.style.background = hl;
  102.         tops[i].checked = true;
  103.         }
  104.         updateTotal();
  105.     });
  106. }
  107.    
  108. // get reference to element containing sizes radiobuttons
  109. var rb = document.getElementById("sizes");
  110.  
  111. // get reference to input elements in toppings container element
  112. var sizes = rb.getElementsByTagName('input');
  113.  
  114. // assign updateChecks function to onclick property of each checkbox
  115. for (var i=0, len=sizes.length; i<len; i++) {
  116.     sizes[i].onclick = updateTotal;
  117. }
  118.  
  119. // called onclick of toppings checkboxes
  120. function updateTotal(e) {
  121.     var size = "";
  122.     var text = "";
  123.     var val = 0.009;
  124.    
  125.     // to show which size got selected
  126.     for (var i=0, len=sizes.length; i<len; i++) {
  127.         if ( sizes[i].checked ) {
  128.             size = sizes[i].id;
  129.             val += parseFloat(sizes[i].value);
  130.         }
  131.     }
  132.    
  133.     // to show which toppings were selected
  134.     for (var i=0, len=tops.length; i<len; i++) {
  135.         if ( tops[i].checked ) {
  136.             text += tops[i].name + "<br>";
  137.             val += parseFloat(tops[i].value);
  138.         }
  139.     }
  140.     if ( text === "" ) {
  141.         text = "***";
  142.     }
  143.    
  144.     document.getElementById("pizza").innerHTML = size;
  145.     document.getElementById("items").innerHTML = text.toUpperCase();
  146.    
  147.     // formats val to 2 decimal places
  148.     val = "" + parseInt(val*100)
  149.     val = val.slice(0,-2) + "." + val.slice(-2,);
  150.    
  151.     // format val with correct number of decimal places
  152.     // and use it to update value of total text box
  153.     document.getElementById("total").innerHTML = val;
  154. }
  155.    
  156.  
  157.  
  158. </script>
  159.  
  160. </body>
  161.  
  162. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement