-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatButtonScript.js
47 lines (41 loc) · 1.52 KB
/
ChatButtonScript.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
var chatbox = document.getElementById('chatbox');
var chatIcon = document.getElementById('chatbox-icon');
var chatBody = document.getElementById('chatbox-body');
var chatInput = document.getElementById('chat-input');
var sendBtn = document.getElementById('send-btn');
// Function to toggle chatbox visibility
chatIcon.addEventListener('click', function () {
if (chatbox.classList.contains('active')) {
chatbox.classList.remove('active');
} else {
chatbox.classList.add('active');
}
});
// Function to create message bubbles
function appendMessage(message, sender) {
var messageDiv = document.createElement('div');
messageDiv.classList.add('message', sender);
var messageContent = document.createElement('div');
messageContent.classList.add('message-content');
messageContent.textContent = message;
messageDiv.appendChild(messageContent);
chatBody.appendChild(messageDiv);
chatBody.scrollTop = chatBody.scrollHeight; // Auto-scroll to bottom
}
// Event listener for the send button
sendBtn.addEventListener('click', function () {
var message = chatInput.value;
if (message.trim() !== '') {
appendMessage(message, 'user');
setTimeout(function () {
appendMessage('This is a bot response', 'bot');
}, 500); // Simulate a bot response delay
chatInput.value = ''; // Clear input field
}
});
// Enter key also sends a message
chatInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendBtn.click();
}
});