-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
21 lines (17 loc) · 733 Bytes
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let input = document.querySelector('input');
let button = document.querySelector('button');
let conversation = document.querySelector('.conversation');
// Retrieve the conversation history from local storage
let history = JSON.parse(localStorage.getItem('conversation')) || [];
// Display the conversation history
for (let i = 0; i < history.length; i++) {
conversation.innerHTML += `<p>${history[i]}</p>`;
}
button.addEventListener('click', function() {
let message = input.value;
conversation.innerHTML += `<p>You: ${message}</p>`;
input.value = '';
// Save the conversation to local storage
history.push(`You: ${message}`);
localStorage.setItem('conversation', JSON.stringify(history));
});