-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
26 lines (19 loc) · 903 Bytes
/
script.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
const chatInput = document.querySelector(".chat-input textarea");
const sendChatBtn = document.querySelector(".chat-input span");
const chatbox = document.querySelector(".chatbox");
let userMessage;
const createChatLi = (message, className) => {
//Create a chat <li> element with passed message and className
const chatLi = document.createElement("li");
chatLi.classList.add("chat", className);
let chatContent = className === "outgoing" ? `<p>${message}</p>` : `<span class="material-symbols-outlined">smart_toy</span><p>${message}</p>`;
chatLi.innerHTML = chatContent;
return chatLi;
}
const handleChat = () => {
userMessage= chatInput.value.trim();
if(!userMessage) return;
//Append the user's message to the chatbox
chatbox.appendChild(createChatLi(userMessage, "outgoing"));
}
sendChatBtn.addEventListener("click", handleChat);