document.addEventListener('DOMContentLoaded', function() { const userInput = document.getElementById('user-input'); const sendButton = document.getElementById('send-button'); const chatMessages = document.getElementById('chat-messages'); // Simple bot responses const botResponses = { 'hello': 'Hello! How can I help you?', 'hi': 'Hi there! What\'s up?', 'bye': 'Goodbye! Have a great day!', 'default': 'Sorry, I didn\'t understand that. Try saying hello!' }; function addMessage(text, sender) { const messageDiv = document.createElement('div'); messageDiv.classList.add('message', sender); messageDiv.textContent = text; chatMessages.appendChild(messageDiv); chatMessages.scrollTop = chatMessages.scrollHeight; } function getBotResponse(input) { const lowerInput = input.toLowerCase(); return botResponses[lowerInput] || botResponses['default']; } function handleSend() { const message = userInput.value.trim(); if (message) { addMessage(message, 'user'); const response = getBotResponse(message); setTimeout(() => addMessage(response, 'bot'), 500); // Simulate delay userInput.value = ''; } } sendButton.addEventListener('click', handleSend); userInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { handleSend(); } }); });