/* Ostoslista-harkka */ $(document).ready(function(){ var groceryList = new Array(); var grocery; var teksti; // add example content grocery = addGrocery("Milk", "2", "0.95"); groceryList.push(grocery); grocery = addGrocery("Butter", "1", "3"); groceryList.push(grocery); grocery = addGrocery("Bread", "2", "2.5"); groceryList.push(grocery); grocery = addGrocery("Chocolate bar (dark)", "2", "3"); groceryList.push(grocery); grocery = addGrocery("Beer", "6", "6"); groceryList.push(grocery); // print list printGroceryList(); addDeleteGroceryButtons(); addEditGroceryOnClickToText(); // new item id $("#theForm #itemId").val(groceryList.length); // add form values to grocery list $("#theButton").click(function() { // read form values grocery = addGrocery( $("#theForm #name").val(), $("#theForm #amount").val(), $("#theForm #price").val() ); // edit existing, if given id is lower than list's length var id = $("#theForm #itemId").val(); if (id < groceryList.length) { groceryList[id] = grocery; changeFormButtonText("Add"); $("#theForm #fromModeText").html("Add new grocery"); } // otherwise add new grocery else { groceryList.push(grocery); } // print list printGroceryList(); // new item id $("#theForm #itemId").val(groceryList.length); // clear form $("#theForm #name").val(""); $("#theForm #amount").val(""); $("#theForm #price").val(""); }); // reset grocery list, empty array and empty div $("#resetGroceryList").click(function() { groceryList = []; $("#groceryList").empty(); }); // add grocery to array function addGrocery(name, amount, price) { return {name:name, amount:amount, price:price}; } // print groceries to div, with delete button function printGroceryList() { $("#groceryList").empty(); var totalPrice = 0; $.each(groceryList, function(index, grocery) { //teksti = "
" + grocery.name + " " + grocery.amount + "kpl " + grocery.price + "e
"; // grocery teksti = "" teksti = teksti.concat("" + grocery.name + " "); teksti = teksti.concat("" + displayAmount(grocery.amount) + " "); teksti = teksti.concat("" + displayPrice(grocery.price) + " "); teksti = teksti.concat("= " + displayTotalPrice(grocery.amount, grocery.price) + " "); teksti = teksti.concat("
"); // delete button button = ""; // add to list $("#groceryList").append('