Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- var basket = [];
- function Pizza() {
- this.id = Math.random();
- this.name = document.getElementById('pizza-name').value || "Name";
- this.desc = document.getElementById('pizza-desc').value || "Desc";
- this.cost = document.getElementById('pizza-cost').value || Math.round(Math.random()*100)%30;
- this.count = 0;
- var elem = document.createElement("tr");
- var tdName = document.createElement("td");
- tdName.innerHTML = this.name;
- var tdDesc = document.createElement("td");
- tdDesc.innerHTML = this.desc;
- var tdCost = document.createElement("td");
- tdCost.innerHTML = this.cost;
- var tdCount = document.createElement("td");
- tdCount.innerHTML = this.count;
- tdCount.id = this.id;
- var btnAdd = document.createElement("td");
- btnAdd.innerHTML += "<button onclick='Add("+this.id+")'>Add</button>";
- btnAdd.innerHTML += "<button onclick='Remove("+this.id+")'>Remove</button>"
- elem.appendChild(tdName);
- elem.appendChild(tdDesc);
- elem.appendChild(tdCost);
- elem.appendChild(tdCount);
- elem.appendChild(btnAdd);
- document.getElementById('tbody').appendChild(elem);
- }
- function Add(id) {
- for (var i = 0; i < basket.length; i++) {
- if (basket[i].id == id)
- {
- basket[i].count++;
- }
- }
- Refresh();
- }
- function Remove(id) {
- for (var i = 0; i < basket.length; i++) {
- if (basket[i].id == id && basket[i].count>0)
- {
- basket[i].count--;
- }
- }
- Refresh();
- }
- function CreatePizza() {
- basket.push(new Pizza());
- document.getElementById('pizza-name').value = "";
- document.getElementById('pizza-desc').value = "";
- document.getElementById('pizza-cost').value = "";
- }
- function Refresh() {
- basket.forEach(function (item) {
- document.getElementById(item.id).innerHTML = item.count;
- });
- }
- window.onload = function () {
- };
- </script>
- <input id="pizza-name"><br>
- <input id="pizza-desc"><br>
- <input id="pizza-cost" type="number"><br>
- <button id="add-pizza" onclick="CreatePizza()">Add neew pizza 2 system</button>
- <h3>Basket</h3>
- <table>
- <thead>
- <tr>
- <td>Name</td>
- <td>Desc</td>
- <td>Cost</td>
- <td>Count</td>
- <td>Controls</td>
- </tr>
- </thead>
- <tbody id="tbody">
- </tbody>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement