Skip to content

Commit fd53821

Browse files
committed
[EX-17.11/st-compl] cr-adding-obj-to-localStorage
Creating "obj/message", adding it to "localStorage". Worth noting: - importance of using the "JSON.stringify()" method. FS-dev: B-3 / JS basic
1 parent 9f9a5c1 commit fd53821

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>DOM</title>
9+
<link rel="stylesheet" href="./styles.css">
10+
<script src="./index.js" defer></script>
11+
</head>
12+
13+
<body style="background-color: #1c1b21; text-align: center">
14+
<img src="./logo.svg" alt>
15+
<div class="panel">I love this!</div>
16+
<div class="input-wrap">
17+
<input class="input" id="input">
18+
<button class="button">Change</button>
19+
</div>
20+
<div class="notification notification_hide">Changed!</div>
21+
<div class="new-panel panel"></div>
22+
</body>
23+
24+
</html>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const panelMessage = document.querySelector('.panel');
4+
const input = document.querySelector('.input');
5+
const button = document.querySelector('.button');
6+
const notificationMessage = document.querySelector('.notification');
7+
let notificationTimer; // переменная для хранения ID таймера
8+
9+
// DRY
10+
function changeMessage() {
11+
const inputValue = input.value;
12+
13+
if (!inputValue) {
14+
return;
15+
}
16+
17+
panelMessage.textContent = inputValue;
18+
input.value = '';
19+
notificationMessage.classList.remove('notification_hide');
20+
21+
const messageObj = {
22+
text: inputValue,
23+
};
24+
25+
// добавление в localStorage
26+
localStorage.setItem('Message', JSON.stringify(messageObj));
27+
28+
// очищение предыдущего таймер
29+
clearTimeout(notificationTimer);
30+
31+
// установка нового таймера (занесение в переменную для ID таймера)
32+
notificationTimer = setTimeout(() => {
33+
notificationMessage.classList.add('notification_hide');
34+
}, 1000);
35+
}
36+
37+
// изменение сообщения через "click" по кнопке
38+
function changeMessageByClick() {
39+
changeMessage();
40+
}
41+
42+
button.addEventListener('click', changeMessageByClick);
43+
44+
// изменение сообщения через "keydown/Enter" в поле input
45+
function changeMessageByKeydown(event) {
46+
// без объекта event не обойтись, т.к. в нём определяем code кнопки.. "Enter"
47+
if (event.code === 'Enter') {
48+
changeMessage();
49+
}
50+
}
51+
52+
input.addEventListener('keydown', changeMessageByKeydown);
53+
54+
// создание/добавление "нового" Html элемента/узла
55+
const newPanel = document.querySelector('.new-panel');
56+
const message = document.createElement('p');
57+
58+
message.classList.add('panel-message');
59+
message.setAttribute('id', 'panel-message');
60+
message.textContent = 'Lets do it!';
61+
62+
newPanel.append(message);
Lines changed: 25 additions & 0 deletions
Loading
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
.panel {
2+
margin-bottom: 20px;
3+
border-radius: 10px;
4+
padding: 40px;
5+
font-size: 18px;
6+
color: white;
7+
background: rgba(255, 255, 255, .05);
8+
}
9+
10+
.input-wrap {
11+
margin-bottom: 20px;
12+
}
13+
14+
.input {
15+
border: 1px solid rgba(212, 212, 212, .05);
16+
border-radius: 10px;
17+
padding: 15px 16px;
18+
height: 56px;
19+
box-sizing: border-box;
20+
font-size: 18px;
21+
line-height: 21px;
22+
color: white;
23+
box-shadow: none;
24+
outline: none;
25+
background: rgba(255, 255, 255, .05);
26+
}
27+
28+
.button {
29+
display: inline-flex;
30+
justify-content: center;
31+
align-items: center;
32+
border: none;
33+
border-radius: 10px;
34+
padding: 20px 40px;
35+
box-sizing: border-box;
36+
text-align: center;
37+
color: white;
38+
background: #6c38cc;
39+
cursor: pointer;
40+
transition: all .2s;
41+
}
42+
43+
.button:hover {
44+
background-color: #844fe6;
45+
}
46+
47+
.notification {
48+
margin-bottom: 20px;
49+
font-size: 16px;
50+
color: white;
51+
}
52+
53+
.notification_hide {
54+
display: none;
55+
}
56+
57+
.panel-message {
58+
margin: 0;
59+
}

0 commit comments

Comments
 (0)