├── README.md ├── app.js ├── images ├── chatbot.png ├── cwt.jpg └── send.svg ├── index.html ├── response.js └── style.css /README.md: -------------------------------------------------------------------------------- 1 | Detailed Explanation Video :https://youtu.be/C2bKXt1yuPE 2 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const chatBody = document.querySelector(".chat-body"); 2 | const txtInput = document.querySelector("#txtInput"); 3 | const send = document.querySelector(".send"); 4 | 5 | send.addEventListener("click", () => renderUserMessage()); 6 | 7 | txtInput.addEventListener("keyup", (event) => { 8 | if (event.keyCode === 13) { 9 | renderUserMessage(); 10 | } 11 | }); 12 | 13 | const renderUserMessage = () => { 14 | const userInput = txtInput.value; 15 | renderMessageEle(userInput, "user"); 16 | txtInput.value = ""; 17 | setTimeout(() => { 18 | renderChatbotResponse(userInput); 19 | setScrollPosition(); 20 | }, 600); 21 | }; 22 | 23 | const renderChatbotResponse = (userInput) => { 24 | const res = getChatbotResponse(userInput); 25 | renderMessageEle(res); 26 | }; 27 | 28 | const renderMessageEle = (txt, type) => { 29 | let className = "user-message"; 30 | if (type !== "user") { 31 | className = "chatbot-message"; 32 | } 33 | const messageEle = document.createElement("div"); 34 | const txtNode = document.createTextNode(txt); 35 | messageEle.classList.add(className); 36 | messageEle.append(txtNode); 37 | chatBody.append(messageEle); 38 | }; 39 | 40 | const getChatbotResponse = (userInput) => { 41 | return responseObj[userInput] == undefined 42 | ? "Please try something else" 43 | : responseObj[userInput]; 44 | }; 45 | 46 | const setScrollPosition = () => { 47 | if (chatBody.scrollHeight > 0) { 48 | chatBody.scrollTop = chatBody.scrollHeight; 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /images/chatbot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewelltech/chatbot-css-js-1/a77fbabe9f84f554ecda9475b0b3890ed4eb599c/images/chatbot.png -------------------------------------------------------------------------------- /images/cwt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewelltech/chatbot-css-js-1/a77fbabe9f84f554ecda9475b0b3890ed4eb599c/images/cwt.jpg -------------------------------------------------------------------------------- /images/send.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 |