-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
152 lines (142 loc) · 4.47 KB
/
index.html
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html>
<head>
<title>Проект "Комменты"</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<ul id="list" class="comments">
<li class="comment">
<div class="comment-header">
<div>Глеб Фокин</div>
<div>12.02.22 12:18</div>
</div>
<div class="comment-body">
<div class="comment-text">
Это будет первый комментарий на этой странице
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">3</span>
<button class="like-button"></button>
</div>
</div>
</li>
<li class="comment">
<div class="comment-header">
<div>Варвара Н.</div>
<div>13.02.22 19:22</div>
</div>
<div class="comment-body">
<div class="comment-text">
Мне нравится как оформлена эта страница! ❤
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">75</span>
<button class="like-button -active-like"></button>
</div>
</div>
</li>
</ul>
<div class="add-form">
<input
type="text"
id="name-input"
value=""
class="add-form-name"
placeholder="Введите ваше имя"
/>
<textarea
type="textarea"
id="comment-input"
value=""
class="add-form-text"
placeholder="Введите ваш коментарий"
rows="4"
></textarea>
<div class="add-form-row">
<button id="add-button" class="add-form-button">Написать</button>
</div>
</div>
</div>
</body>
<style>
.error {
background-color: darkred;
}
</style>
<script>
"use strict";
const buttonElement = document.getElementById("add-button");
const listElement = document.getElementById("list");
const inputNameElement = document.getElementById("name-input");
const inputTextElement = document.getElementById("comment-input");
function toggleButton() {
if (inputNameElement.value && inputTextElement.value) {
buttonElement.disabled = false;
buttonElement.classList.remove("disabled");
} else {
buttonElement.disabled = true;
buttonElement.classList.add("disabled");
}
}
inputNameElement.addEventListener("input", toggleButton);
inputTextElement.addEventListener("input", toggleButton);
buttonElement.addEventListener("click", () => {
inputNameElement.classList.remove("error");
inputTextElement.classList.remove("error");
if (inputNameElement.value === "") {
inputNameElement.classList.add("error");
return;
}
if (inputTextElement.value === "") {
inputTextElement.classList.add("error");
return;
}
const currentDate = new Date().toLocaleString("ru-RU", {
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
day: "2-digit",
month: "2-digit",
year: "2-digit",
});
listElement.innerHTML += `
<li class="comment">
<div class="comment-header">
<div>${inputNameElement.value}</div>
<div>${currentDate}</div>
</div>
<div class="comment-body">
<div class="comment-text">
${inputTextElement.value}
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">0</span>
<button class="like-button -active-like"></button>
</div>
</div>
</li>
`;
inputNameElement.value = "";
inputTextElement.value = "";
buttonElement.disabled = true;
buttonElement.classList.add("disabled");
});
inputTextElement.addEventListener("keyup", (event) => {
if (event.keyCode === 13 && !buttonElement.disabled) {
event.preventDefault();
buttonElement.click();
}
});
</script>
</html>
<!--