0

Hey I am making a chat bot but I want that when you press enter or click on button that the div scrolls to bottom, Because when you chat with the bot you need to scroll down ever time. I have a function but the scroll must be in the talk() function.

This is the template code

<template>
    <div class="container">
        <div class="container max">
            <div class="container con">
                <div class="row ml-0 mt-3">
                    <p>{{this.know.welkom}}</p>
                </div>
               <p id="chatLog" class="chatLog font-weight-bold"></p><br>
            </div>
        </div>
        <div class="row mt-4">
            <div class="col">
                <input id="userBox" class="inputChat" type="text" @keyup.enter="talk()" v-model="msg" required>
            </div>
            <div type="submit" value="Send" class="btn btn-primary mt-2 ml-2" @click="talk()">Send<img class="sendIcon" src="@/assets/icons/send.png"></div>
        </div>
    </div>
</template>

And this is my script

import json from './info.json';
export default {
    data() {
        return {
            title: 'Chat bot',
            test: this.json,
            know: {
                'welkom' : '📍Welcome I will help you to answer your questions. If you want more info type "help".📍',
                'help' : 'These are the questions that can be answerd: <br>' + 
                         '1️⃣ how do you login <br>' +
                         '2️⃣ how do you register <br>' +
                         '3️⃣ how to add a internship <br>' +
                         '4️⃣ edit a internship <br>' +
                         '5️⃣ who are you',
                'how do you login' : 'If you are on the "Homepage" you see on the top a "Login" button click on it, ' + 
                                     'sign in with your email and password and you logged in.',
                'how do you register' : '1. Click on "login" on the navbar.' +
                                        '2. On the right side you see a "SIGN UP" button click on the button and you can register.',
                'how to add a internship' : '1. You need a account and you need to be logged in.' + '<br>' +
                                            '2. When you logged in you see above your email on the right side you see "Add internship" ' +
                                            'click on it.' + '<br>' +
                                            '3. Then you see some empty field you need to fill with your company information ' +
                                            '4. You can see how it is on the "Preview design" and if everthing is good click on "Add internship".',
                'edit a internship' : '1. Go to your "internship page". Their you see a edit button if you click on it you can edit ' +
                                      'your internship information.',
                'who are you' : 'I am your DAD! Look behind 😀'
            },
            msg: ''
        }
    },
    head: {
        title: function () {
            return {
                inner: this.title
            }
        }
    },
    methods: {
        talk() {
            var user = this.msg;
            document.getElementById('chatLog').innerHTML += user + '<br>';
            if (user != '') {
                if (user in this.know) {
                    document.getElementById('chatLog').innerHTML += this.know[user] + "<br>" + "<br>";
                } else {
                    document.getElementById('chatLog').innerHTML += 'I don\'t understand...<br>' + '<br>';
                }
            } else {
                alert('Type text in');
            }
            this.msg = '';
            console.log(this.msg);
        }
    }
}

I used scrollheight between the this.msg = ''; and console.log(this.msg); see the talk() function


I added the scrollheight but it doesn't scroll automatically:

enter image description here

And this is the function code when I paste it:

talk() {
  var user = this.msg;
  document.getElementById('chatLog').innerHTML += user + '<br>';
  if (user != '') {
     if (user in this.know) {
        document.getElementById('chatLog').innerHTML += this.know[user] + "<br>" + "<br>";
     } else {
        document.getElementById('chatLog').innerHTML += 'I don\'t understand...<br>' + '<br>';
     }
     } else {
       alert('Type text in');
     }
     console.log(this.msg);
     this.msg = '';
     const chatLogDiv = document.getElementById('chatLog')
     chatLogDiv.scrollTop = chatLogDiv.scrollHeight
}

1 Answer 1

1

To scroll to the bottom of the #chatLog div just do:

const chatLogDiv = document.getElementById('chatLog')
chatLogDiv.scrollTop = chatLogDiv.scrollHeight
Sign up to request clarification or add additional context in comments.

5 Comments

It didn't scroll take a look on the picture and the code I added now.
It looks like your chatLog element is empty. Which element is meant to be scrolling? Add ref="scroll" to the element and select with this.$refs.scroll
This element must be scrolled: class="container con"
so change to: const chatLogDiv = this.$refs.scroll and add ref="scroll" to the element
Thanks it works! I put the ref="scroll" in the class="container max" element and now it scrolls to the bottom of the div.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.