-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (40 loc) · 1.56 KB
/
script.js
File metadata and controls
49 lines (40 loc) · 1.56 KB
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
<script>
// Function to send a message
function sendMessage() {
const chatContainer = document.getElementById('chatContainer');
const chatInput = document.getElementById('chatInput');
const messageText = chatInput.value.trim();
if (messageText !== '') {
// Create a new message element
const message = document.createElement('div');
message.classList.add('message', 'sender');
message.textContent = messageText;
// Append the message to the chat container
chatContainer.appendChild(message);
// Scroll to the bottom
chatContainer.scrollTop = chatContainer.scrollHeight;
// Clear the input field
chatInput.value = '';
// Simulate a response
setTimeout(() => generateResponse(messageText), 1000);
}
}
// Function to simulate a response
function generateResponse(userMessage) {
const chatContainer = document.getElementById('chatContainer');
const responses = [
'Interesting!',
'Can you tell me more?',
'I agree with that.',
'That sounds amazing!',
'I didn’t know that, thanks for sharing!'
];
// Create a new response message
const response = document.createElement('div');
response.classList.add('message', 'receiver');
response.textContent = responses[Math.floor(Math.random() * responses.length)];
// Append the response to the chat container
chatContainer.appendChild(response);
// Scroll to the bottom
chatContainer.scrollTop = chatContainer.scrollHeight;
}