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...

Aama ko Sapana Exercise : Question Answers and Grammar | Neb Class 12

  Aama ko Sapana Exercise : Question Answers and Grammar | Mother's dream Shishir bhattarai , 2024   Lesson 1 Mother's dream vocabulary 1. Pair between words that have the same meaning: Usha  – Bright dreams  – Hope for a happy future or imagination Youth  – Youth – Youth  – Struggling struggle  – Fight inertia  – Foolishness –  Throat hope  – Desire to get what you want 2. Search and write a word that matches the meaning from the text: (a) Small drops of water falling on the ground, trees, leaves, etc. at night:  Cold   (b) Instrument made from bamboo, swallow, etc. played by turning it to the right groin with  the lips:  Flute (c) Ability to take or hold:  Ability to create eclipse (d) Creativity (e) To be satisfied or to be satisfied :  Consolation (f) Wind speeding:  Wind 3. Write a synonym for the given word and use them in a sentence: Air : Air pollution is  heavy, so it remains in the city f...

Can Sunlight Cook an Omelette? ЁЯН│☀️рдХे рдШाрдорд▓े рдЖрдорд▓ेрдЯ рдкрдХाрдЙрди рд╕рдХ्рдЫ? |

  In this video, we're taking on an extreme cooking challenge: can we cook an omelette using only the power of the sun? Join us as we attempt this daring experiment, utilizing solar cooking techniques and harnessing the sun's rays to create a delicious breakfast. Will we succeed in creating a perfectly cooked omelette without a stove? Find out in this exciting and informative video! [Nepali Translation:] рдпрд╕ рднिрдбिрдпोрдоा, рд╣ाрдоी рдПрдЙрдЯा рдЪрд░рдо рдЦाрдиा рдкрдХाрдЙрдиे рдЪुрдиौрддी рд▓िँрджैрдЫौं: рдХे рд╣ाрдоी рдШाрдордХो рд╢рдХ्рддि рдоाрдд्рд░ рдк्рд░рдпोрдЧ рдЧрд░ेрд░ рдЖрдорд▓ेрдЯ рдкрдХाрдЙрди рд╕рдХ्рдЫौं? рд╣ाрдоीрд▓ाрдИ рд╕ाрдоेрд▓ рд╣ुрдиुрд╣ोрд╕् рдЬрдм рд╣ाрдоी рдпो рд╕ाрд╣рд╕ी рдк्рд░рдпोрдЧрдХो рдк्рд░рдпाрд╕ рдЧрд░्рдЫौं, рд╕ौрд░्рдп рдЦाрдиा рдкрдХाрдЙрдиे рдк्рд░рд╡िрдзिрд╣рд░ू рдк्рд░рдпोрдЧ рдЧрд░ेрд░ рд░ рд╕्рд╡ाрджिрд╖्рдЯ рдиाрд╢्рддा рдмрдиाрдЙрди рд╕ूрд░्рдпрдХो рдХिрд░рдгрд▓ाрдИ рдк्рд░рдпोрдЧ рдЧрд░्рдЫौं। рдХे рд╣ाрдоी рдЪुрд▓ो рдмिрдиा рдкूрд░्рдг рд░ूрдкрдоा рдкрдХाрдПрдХो рдЖрдорд▓ेрдЯ рдмрдиाрдЙрди рд╕рдлрд▓ рд╣ुрдиेрдЫौं? рдпो рд░ोрдоाрдЮ्рдЪрдХ рд░ рдЬाрдирдХाрд░ीрдоूрд▓рдХ рднिрдбिрдпोрдоा рдкрдд्рддा рд▓рдЧाрдЙрдиुрд╣ोрд╕्!