<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<head>
<title>My Very First Web Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<h1>My Bakery List</h1>
<ol id="bakeryList">
</ol>
<input id="bakeryItemNameInput"placeholder="Input Bakery Item"type="text"/>
<input id="bakeryItemCostInput"placeholder="Input Bakery Item Cost"type="text"/>
<button id="addItemButton"> Add Bakery Item</button>
<p></p>
<input id="bakeryItemNameInputMod"placeholder="Input Bakery Item to Mod"type="text"/>
<input id="modifyCostInput"placeholder="New Cost of Bakery Item"type="text"/>
<button id="modifyCostButton"> Modify Cost</button>
<script>
var bakery = [];
function redrawList(){
var list = $("#bakeryList");
list.html("");
$.each(bakery,function(index,value){
$("<li/>").text(value.name+", "+value.cost+" Euro").appendTo(list);
});
}redrawList();
$("#addItemButton").click(function(){
var bakeryItemName = $("#bakeryItemNameInput").val();
var bakeryItemCost = $("#bakeryItemCostInput").val();
var bakeryObject = {name:bakeryItemName,cost:parseInt(bakeryItemCost)};
bakery.push(bakeryObject);
console.log(bakery);
redrawList();
});
var bakery = [];
function redrawModList(){
var list = $("#bakeryList");
list.html("");
$.each(bakery,function(index,value){
$("<li/>").text(value.name+", "+value.cost+" Euro").appendTo(list);
});
};
$("#modifyCostButton").click(function(){
var bakeryItemToChange = $("#bakeryItemNameInputMod").val();
var modifyBakeryItemCost = $("#modifyCostInput").val();
var bakeryObjectMod = {name:bakeryItemToChange,cost:parseInt(modifyBakeryItemCost)};
var x = bakery.indexOf({bakeryObjectMod});
bakery.splice(x,1,{name:bakeryItemToChange,cost:parseInt(modifyBakeryItemCost)});
console.log(x); //always -1
redrawList();
});
</script>
</body>
</html>
Not sure why this message field didn’t put in my entire code block