Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (77 loc) · 2.51 KB
/
Copy pathindex.js
File metadata and controls
97 lines (77 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(function () {
//........................ HW BUILDER - 01 START .....................
//__________________________________ all variable ______________________________
const pName = document.getElementById('product-name');
const pQuant = document.getElementById('product-quantity');
const pBrand = document.getElementById('product-brand');
const inpFrm = document.getElementById('input-form');
const outputSide = document.getElementById('output-box');
const rmvBtn = document.getElementById('remove');
//__________________________________ all EventListener ______________________________
function eventListenerLoader() {
document.addEventListener('DOMContentLoaded', loadDataFunction);
inpFrm.addEventListener('submit', submitData);
rmvBtn.addEventListener('click', removeFunction)
}
eventListenerLoader();
//__________________________________ all Function ______________________________
function submitData(e) {
e.preventDefault();
if (pName.value === '' || pQuant.value === '' || pBrand.value === '') {
alert('Put All Information');
return;
}
const pData = {
name : pName.value,
quantity : pQuant.value,
brand : pBrand.value
}
const formattedTxt = formatData(pData);
saveDataToStorage(pData);
outputSide.innerHTML += formattedTxt;
pName.value = '';
pQuant.value = '';
pBrand.value = '';
};
function formatData({name, quantity, brand}){
return `
<div class="card" id="card">
<div class="contentBox">
<h3 >${name}</h3>
<h2 class="quantity">${quantity}</h2>
<h2 class="brand">${brand}</h2>
<button class="remove">REMOVE</button>
</div>
</div>
`;
};
function saveDataToStorage(pData) {
let allItems;
if (localStorage.getItem('itemData')) {
allItems = JSON.parse(localStorage.getItem('itemData'))
} else {
allItems = [];
}
allItems.push(pData);
localStorage.setItem('itemData', JSON.stringify(allItems));
}
function loadDataFunction() {
let allItems;
if (localStorage.getItem('itemData'))
{
allItems = JSON.parse(localStorage.getItem('itemData'))
} else
{
allItems = [];
}
let formattedTxt = '';
allItems.forEach(allItems => {
formattedTxt += formatData(allItems)
});
outputSide.innerHTML = formattedTxt;
}
function removeFunction() {
localStorage.removeItem('itemData');
}
//........................ HW BUILDER - 01 END .....................
})();