-
Notifications
You must be signed in to change notification settings - Fork 32
/
app.js
51 lines (42 loc) · 1.19 KB
/
app.js
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
const userMessages = [];
const userMessageForm = document.querySelector('form');
const userMessagesList = document.querySelector('ul');
function renderMessages() {
let messageItems = '';
for (const message of userMessages) {
messageItems = `
${messageItems}
<li class="message-item">
<div class="message-image">
<img src="${message.image}" alt="${message.text}">
</div>
<p>${message.text}</p>
</li>
`;
}
userMessagesList.innerHTML = messageItems;
}
function formSubmitHandler(event) {
event.preventDefault();
const userMessageInput = event.target.querySelector('textarea');
const messageImageInput = event.target.querySelector('input');
const userMessage = userMessageInput.value;
const imageUrl = messageImageInput.value;
if (
!userMessage ||
!imageUrl ||
userMessage.trim().length === 0 ||
imageUrl.trim().length === 0
) {
alert('Please insert a valid message and image.');
return;
}
userMessages.push({
text: userMessage,
image: imageUrl,
});
userMessageInput.value = '';
messageImageInput.value = '';
renderMessages();
}
userMessageForm.addEventListener('submit', formSubmitHandler);