Skip to main content

Creating a Chatbot with HTML Code

Creating a Chatbot with HTML Code

Chatbots have become an essential part of modern-day communication, making it easy to interact with customers, provide support, and automate tasks. In this article, we will create a simple chatbot using HTML code and JavaScript.

Prerequisites

To create a chatbot with HTML code, you need to have basic knowledge of HTML and JavaScript. You should also have a text editor or an integrated development environment (IDE) installed on your computer.

Step 1: Setting up the HTML Code

The first step is to create an HTML file and set up the basic structure. Here's a simple code to get started:


```
<!DOCTYPE html>
<html>
<head>
<title>Chatbot</title>
</head>
<body>
<div id="chatbot-container">
<div id="chatbot-output"></div>
<input type="text" id="chatbot-input" placeholder="Type your message here...">
</div>
<script src="app.js"></script>
</body>
</html>
```

In this code, we have created a basic structure of an HTML page. We have also created a container for the chatbot, which consists of an output area and an input area for the user to type their messages. We have also linked a JavaScript file called app.js to the HTML file.

Step 2: Writing the JavaScript Code

Now, let's create the app.js file and write the code for our chatbot. Here's the code:

```
const output = document.getElementById("chatbot-output");
const input = document.getElementById("chatbot-input");

const chatbot = [
    {
        question: "Hello, how can I help you today?",
        answer: "I need help with my order.",
    },
    {
        question: "What is your order number?",
        answer: "My order number is 12345.",
    },
    {
        question: "What seems to be the problem with your order?",
        answer: "I received the wrong item.",
    },
    {
        question: "We apologize for the inconvenience caused. We will process a refund for you.",
        answer: "Thank you for your assistance.",
    },
];

let currentQuestion = 0;

function askQuestion() {
    output.innerHTML += "<div class='question'>" + chatbot[currentQuestion].question + "</div>";
}

function respond() {
    output.innerHTML += "<div class='answer'>" + chatbot[currentQuestion].answer + "</div>";
    currentQuestion++;
}

input.addEventListener("keydown", function (event) {
    if (event.keyCode === 13) {
        output.innerHTML += "<div class='user-message'>" + input.value + "</div>";
        askQuestion();
        respond();
        input.value = "";
    }
});
```

In this code, we have created an array called chatbot, which contains a list of questions and answers. We have also created functions called askQuestion() and respond() to display the questions and answers on the output area.

The input area is also listening for a keydown event, and when the user presses the enter key, the user's message is displayed on the output area, and the chatbot's response is displayed based on the current question.

Step 3: Styling the Chatbot

Finally, let's add some CSS to style our chatbot. Here's the code:

```
#chatbot-container {
    width: 300px;
    height: 400px;
    border: 1px solid #ccc;
    border-radius: 5px;
    overflow: hidden;
    margin: 0 auto;
    margin-top: 50px;
}

#chatbot-output {
    height: 80%;
    padding: 10px;
    overflow-y: scroll;
}

#chatbot-input {
    width: 100%;
    height: 20%;
    padding: 10px;
    border: none;
    outline: none;
}

.user-message {
    text-align: right;
    margin-top: 10px;
    margin-right: 10px;
}

.question {
    text-align: left;
    margin-top: 10px;
    margin-left: 10px;
    font-weight: bold;
}

.answer {
    text-align: left;
    margin-top: 10px;
    margin-left: 10px;
}
```

In this code, we have added some basic styling to our chatbot container, output area, and input area. We have also styled the user's message, chatbot's question, and chatbot's answer.

Conclusion

In this article, we have learned how to create a simple chatbot using HTML code and JavaScript. You can customize this chatbot by adding more questions and answers, changing the styling, and adding more functionality. With this chatbot, you can provide support, automate tasks, and make communication more efficient.
Chatbot
Hi there! How can I assist you today?

Comments

Popular posts from this blog

Sunita Williams Smiles as She Returns Home After 286 Days in Space

  Sunita Williams Smiles As She Returns Home After 286 Days. Sunita Williams, a renowned NASA astronaut, has made headlines once again with her remarkable journey in space. After spending an extended period of 286 days orbiting Earth, Williams has finally returned home, marking the end of a mission initially planned for just 10 days. This extraordinary feat showcases her resilience and highlights the complexities and challenges of space exploration. The Mission Williams, along with fellow astronaut Butch Wilmore, embarked on the first crewed mission of Boeing's Starliner spacecraft in June 2024. The mission was intended to be a brief test flight to demonstrate Starliner's capability to carry astronauts to and from the International Space Station (ISS). However, due to unforeseen technical issues, the duo found themselves extending their stay on the ISS for nearly nine months 6 7 . The Challenges The Starliner mission faced several setbacks, including malfunctioning thrusters th...

Argentina beat Brazil 1-0 and wins Copa America

 Argentina beat Brazil 1-0 and wins Copa America Argentina captain Lionel Messi won the first international title in his career. Messi won the Copa America on Sunday morning. In a very competitive match, Argentina defeated Brazil 1-0 to win the title. Winger Angel Di Maria scored to give Argentina the title. He scored in the 22nd minute with a long pass from Rodrigo de Paul. Messi previously played four finals for Argentina, but failed to win the title. Argentina won the Copa America for the first time since 1993, failing to reach the final. Argentina reached the 2014 World Cup, 2015 and 2016 Copa America finals. Prior to that, when he was in the team, Argentina had reached the 2007 Copa Cup Final. However, Argentina lost four times in the final.  Argentina Lift Lionel Messi In the air after his First National Trophy Win Copa America 2021 [ VIDEO ]

I Went to School for 18 Years... and Got Pranked by Life!

 Hey, I'm Shishir. I'm from Nepal, and now I'm living in Perth, Australia. And here's something I've been thinking about: I spent 18 years of my life going to school. Yeah, 18. I'm 20 now, and honestly? I'm still trying to figure out if it was all... worth it.  It felt like a full-time job. 10 to 4, every day. Backpack stuffed with textbooks, notebooks spilling out, sometimes even an extra bag. Then home, the real work started: homework, test prep, the constant pressure to get better grades. I was convinced that if I worked hard enough, I'd unlock some secret to success. That was the routine until Grade 10. Then came the big decisions: Morning classes or day classes? (Day classes, obviously. Who wants to freeze in the winter?) And then the even bigger ones: Science, commerce, arts, law? What college? I ended up at a pricey college in Kathmandu, because a relative went there. Looking back, I think, "Why didn't I just go somewhere more affordable...