Commit 950a88c1 by alizad

Initial commit

parents
Showing with 162 additions and 0 deletions
[.ShellClassInfo]
InfoTip=This folder is shared online.
IconFile=C:\Program Files\Google\Drive\googledrivesync.exe
IconIndex=16
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>lab-10</title>
</head>
<body>
<h1 id="caption">AL's Shopping List</h1>
<span>Product Name:</span>
<input type="text" id="item" value="">
<span>Product Count:</span>
<input type="number" id="count" min=1 value=1>
<button type="button" id="addToList">Add</button>
<table id="tbl">
<tr>
<th>Number</th>
<th>Product</th>
<th>Quantity</th>
</tr>
</table>
<script src="./script.js"></script>
</body>
</html>
\ No newline at end of file
function addToList() {
var product = productTF.value;
var quantity = quantityTF.value;
if (product.length > 0) {
addItemToList(data["num"], product, quantity);
let tempItem = {};
tempItem["num"] = data["num"].toString;
tempItem["item"] = product;
tempItem["count"] = quantity;
data[data["num"].toString()] = tempItem;
localStorage.setItem(usernameme, JSON.stringify(data));
data["num"]++;
productTF.value = "";
quantityTF.value = 1;
}
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
function removeItem(itemID) {
if (!confirm("Are you sure?")) return;
var item = document.getElementById(itemID);
item.parentNode.removeChild(item);
data[itemID.toString()] = null;
localStorage.setItem(usernameme, JSON.stringify(data));
}
function addToList(n, p, q) {
var tr = document.createElement("tr");
var noF = document.createElement("td");
var productF = document.createElement("td");
var quantityF = document.createElement("td");
tr.id = n;
noF.innerHTML = n + ".";
productF.innerHTML = p;
quantityF.innerHTML = q;
tr.appendChild(noF);
tr.appendChild(productF);
tr.appendChild(quantityF);
table.appendChild(tr);
tr.addEventListener("click", () => {removeItem(tr.id);});
}
var productTF = document.getElementById("item");
var quantityTF = document.getElementById("count");
var table = document.getElementById("tbl");
var username = getCookie("username");
var data = {
"num" : 1
};
if (typeof(Storage) === "undefined") {
document.getElementById("caption").innerHTML = "ERROR";
} else {
if (usernameme === null || usernameme === "" || usernameme === "null") {
usernameme = sessionStorage.getItem("username");
}
if (usernameme === null || usernameme === "" || usernameme === "null") {
usernameme = prompt("Name");
setCookie("username", usernameme, 60);
sessionStorage.setItem("username", usernameme);
localStorage.setItem(usernameme, JSON.parse(data));
} else if (localStorage.getItem(usernameme) !== null) {
data = JSON.parse(localStorage.getItem(usernameme));
for (const [key, val] of Object.entries(data)) {
if (val !== null && key !== "num") addToList(key, val["item"], val["count"]);
}
}
document.getElementById("caption").innerHTML = usernameme + "'s Shopping List";
document.getElementById("addToList").addEventListener("click", addToList);
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment