It looks like you're building a simple chat application in HTML, CSS, and JavaScript. Below is a complete version of the chat application with JavaScript functionality to send and display messages.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Chat</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
margin: 0;
padding: 20px;
}
#chat-container {
width: 400px;
height: 500px;
border: 1px solid #ccc;
background: #fff;
overflow-y: scroll;
padding: 10px;
}
#message-input {
width: 300px;
padding: 10px;
}
#send-button {
padding: 10px;
}
.message {
margin: 10px 0;
padding: 5px;
border-radius: 5px;
display: inline-block;
max-width: 80%;
}
.sent {
background-color: #DCF8C6;
align-self: flex-end;
margin-left: auto;
}
.received {
background-color: #f1f0f0;
align-self: flex-start;
}
#chat-messages {
display: flex;
flex-direction: column;
}
</style>
</head>
<body>
<div id="chat-container">
<div id="chat-messages"></div>
</div>
<input type="text" id="message-input" placeholder="Type a message..." />
<button id="send-button">Send</button>
<script>
const sendButton = document.getElementById('send-button');
const messageInput = document.getElementById('message-input');
const chatMessages = document.getElementById('chat-messages');
sendButton.addEventListener('click', () => {
const messageText = messageInput.value.trim();
if (messageText) {
sendMessage(messageText);
messageInput.value = ''; // Clear input
}
});
messageInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
const messageText = messageInput.value.trim();
if (messageText) {
sendMessage(messageText);
messageInput.value = ''; // Clear input
}
}
});
function sendMessage(text) {
// Create a new div for the message
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', 'sent');
messageDiv.textContent = text;
// Append the message to the chat
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight; // Scroll to bottom
// Simulate a received message
setTimeout(() => {
receiveMessage("This is an automated response.");
}, 1000);
}
function receiveMessage(text) {
// Create a new div for the received message
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', 'received');
messageDiv.textContent = text;
// Append the message to the chat
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight; // Scroll to bottom
}
</script>
</body>
</html>
Instructions to Run the Code:
Copy the entire HTML code above.
Paste it into a text editor.
Save the file with a .html extension (e.g., chat.html).
Open the saved file in a web browser.
Functionality:
Type a message in the input box and click "Send" or press "Enter" to submit the message.
Upon sending a message, the chat box displays it.
There's also an automated response simulating a reply after 1 second.
The chat box will scroll to the bottom as new messages are added.
Feel free to build upon this structure and add more features such as styling, user names, or back-end data handling!