Skip to content

Commit c9c682b

Browse files
committed
add local storage
1 parent 3681064 commit c9c682b

3 files changed

Lines changed: 44 additions & 37 deletions

File tree

apps/localStorage-and-event-delegation/assets/script/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
const addItems = document.querySelector('.form');
22
const itemsList = document.querySelector('.list');
3+
let itemsLocal = JSON.parse(localStorage.getItem('items')) || [];
34

45
const addItem = e => {
56
e.preventDefault();
67
const text = getInputValue();
78
const element = createElement(text);
89

10+
itemsLocal = [...itemsLocal, text];
911
itemsList.insertAdjacentHTML('beforeend', element);
1012

13+
// itemsList.insertAdjacentHTML('beforeend', useLocalStorage(itemsLocal));
14+
localStorage.setItem('items', JSON.stringify(itemsLocal));
1115
e.target.reset();
1216
};
1317

@@ -29,4 +33,7 @@ const createElement = item => `
2933
</li>
3034
`;
3135

36+
const useLocalStorage = listLocalStorage =>
37+
listLocalStorage.map(item => createElement(item)).join('');
38+
3239
addItems.addEventListener('submit', addItem);

apps/localStorage-and-event-delegation/assets/style/index.css

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ body {
3838
align-self: flex-end;
3939
margin-right: 5%;
4040
border: 0.5px solid #d3dcd5;
41+
overflow: scroll;
4142
}
4243

4344
.content__title {
@@ -48,6 +49,31 @@ body {
4849
color: #9c1f33;
4950
}
5051

52+
.form {
53+
margin: 20px 0;
54+
display: flex;
55+
align-self: flex-start;
56+
}
57+
58+
.form__input {
59+
border-radius: 5px 0 0 5px;
60+
font-size: 0.8em;
61+
font-weight: 100;
62+
transition: 0.3s;
63+
border: 0.5px solid rgba(0, 0, 0, 0.2);
64+
padding: 10px 5px;
65+
}
66+
67+
.form__submite {
68+
text-transform: uppercase;
69+
background: #9c1f33;
70+
color: #f7f7f7;
71+
font-size: 0.8em;
72+
padding: 10px;
73+
border-radius: 0 5px 5px 0;
74+
border: 0.5px solid rgba(0, 0, 0, 0.2);
75+
}
76+
5177
.list {
5278
margin: 0;
5379
padding: 0;
@@ -76,29 +102,3 @@ body {
76102
color: #9c1f33;
77103
font-weight: 500;
78104
}
79-
80-
.form {
81-
margin: 20px 0;
82-
display: flex;
83-
align-self: flex-start;
84-
}
85-
86-
.form__input {
87-
background: #f7f7f7;
88-
border-radius: 5px 0 0 5px;
89-
font-size: 0.8em;
90-
font-weight: 100;
91-
transition: 0.3s;
92-
border: 0.5px solid rgba(0, 0, 0, 0.2);
93-
padding: 10px 5px;
94-
}
95-
96-
.form__submite {
97-
text-transform: uppercase;
98-
background: #9c1f33;
99-
color: #f7f7f7;
100-
font-size: 0.8em;
101-
padding: 10px;
102-
border-radius: 0 5px 5px 0;
103-
border: 0.5px solid rgba(0, 0, 0, 0.2);
104-
}

apps/localStorage-and-event-delegation/index.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,6 @@
6565
<div class="content">
6666
<h1 class="content__title">Fancy foods</h1>
6767

68-
<ul class="list">
69-
<li class="list__item">
70-
<input class="list__item__checkbox" name="item" type="checkbox" />
71-
<label class="list__item__label" for="item">Beef Bourguignon</label>
72-
</li>
73-
<li class="list__item">
74-
<input class="list__item__checkbox" name="item" type="checkbox" />
75-
<label class="list__item__label" for="item">Salad Nicoise</label>
76-
</li>
77-
</ul>
78-
7968
<form class="form">
8069
<input
8170
class="form__input"
@@ -86,6 +75,17 @@ <h1 class="content__title">Fancy foods</h1>
8675
/>
8776
<input class="form__submite" type="submit" value="Add" />
8877
</form>
78+
79+
<ul class="list">
80+
<li class="list__item">
81+
<input class="list__item__checkbox" name="item" type="checkbox" />
82+
<label class="list__item__label" for="item">Beef Bourguignon</label>
83+
</li>
84+
<li class="list__item">
85+
<input class="list__item__checkbox" name="item" type="checkbox" />
86+
<label class="list__item__label" for="item">Salad Nicoise</label>
87+
</li>
88+
</ul>
8989
</div>
9090
</div>
9191
<script src="./assets/script/index.js"></script>

0 commit comments

Comments
 (0)