The chat text will automatically scroll down, displaying information loaded from the database

Is there a way to ensure that chat text always remains scrolled to the bottom of the chat?

I have tried multiple tutorials and examples from Stack Overflow, but none seem to work for me.

Currently, my chat text overflows the textarea when there are too many lines, despite having overflow:hidden set.

I also want the textarea to only load the last 50 entries from the database so that new users don't have to load all previous chat messages.

Any suggestions on how to achieve these two things?

home

<?php
    session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <div id="Holder">
        <div id="Header"></div>
   <link href="../Style/Style.css" type="text/css" rel="stylesheet"/>

    <script type="text/javascript" src="../Js/jquery.js"></script>
    <title>Chat Home</title>
    <script type="text/javascript">
        $(document).ready(function(){

            $("#ChatText").keyup(function(e){
                //When we press Enter do
                if(e.keyCode==13){
                   var ChatText =  $("#ChatText").val();
                    $.ajax({
                        type:'POST',
                        url:'InsertMessage.php',
                        data:{ChatText:ChatText},
                        success:function(){
                            $("#ChatMessages").load("DisplayMessages.php");
                            $("#ChatText").val("");
                        }
                    })
                }
            });
            setInterval(function(){//refreshes every 1500ms
                $("#ChatMessages").load("DisplayMessages.php");
            },1500);
            $("#ChatMessages").load("DisplayMessages.php");

    </script>
</head>
<body>
<h2>Welcome <span style="color: green"><?php echo $_SESSION['UserName'] ; ?></span> </h2>
</br></br>

<div id="ChatBig">
    <div id="ChatMessages">
    </div>
    <input type="text" id="ChatText" name="ChatText"></textarea>
</div>
<div id="Footer"></div>
</div>
</body>

</html>

Css

#ChatBig {
    width: 60%;
    height: 600px;
    margin: auto;
    background-color: white;
    overflow:hidden;
    resize:none;

}
#ChatMessages{
    width: 100%;
    height: 548px;
    border: 1px solid #000;
    font-size: 16px;
    font-weight: normal;
    overflow:hidden;
    resize:none;
}
#ChatText{
    width: 100%;
    height: 50px;
    border: 1px solid #000;
    overflow:hidden;
    resize:none;
}
#ChatText:focus{outline: none;}

body {
    background-color: lightgray;
}
#Holder {
    width: 100%;
}
.UserNameS{
    color: #7CB9E8;
}
.UserNameS:hover{text-decoration:underline; cursor: pointer;}

Image of how the site looks

http://gyazo.com/fe71d7d9699b9784fda5ba1c862fad53

Answer №1

If you prefer not to have scrolling, you can consider using position: absolute; and position: relative;. Here's an example:

#ChatBig {
    width: 60%;
    height: 600px;
    margin: auto;
    background-color: white;
    overflow:hidden;
    resize:none;
    background: red;
    position: relative;
}
#ChatMessages{
    width: 100%;
    min-height: 498px;
    border: 1px solid #000;
    font-size: 16px;
    font-weight: normal;
    overflow:hidden;
    resize:none;
    position: absolute;
    bottom: 50px;
}
#ChatText{
    width: 100%;
    height: 50px;
    border: 1px solid #000;
    overflow:hidden;
    resize:none;
    position: absolute;
    bottom: 0;
}
#ChatText:focus{outline: none;}
<div id="ChatBig">
    <div id="ChatMessages">
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        asd<br/>
        qwe<br/>
    </div>
    <input type="text" id="ChatText" name="ChatText">
</div>

You must set the parent container to position: relative;, and then set the child element to absolute.

Initially, the ChatBig has a height of 600px and the ChatText is 50px, so adjust the ChatMessages like this:

min-height: 498px; // avoid using height
position: absolute;
bottom: 50px; // matching the ChatText height of 50px

Next, for the ChatText, set it as follows:

position: absolute;
bottom: 0;

By doing this, the latest ChatMessages will stay at the bottom.

Note: The "qwe" text is simply used to mark the last line.

Answer №2

Can you explain the meaning of this particular line?

<input type="text" id="ChatText" name="ChatText"></textarea>

Should it actually be:

<textarea id="ChatText" name="ChatText"></textarea>

or maybe it should be:

<input type="text" id="ChatText" name="ChatText"/>

Answer №3

The solution is to include the max-height attribute.

.element
{
    max-height: 200px;
    overflow-y: auto;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS for Adjusting Parent Height Based on Child Content

I've been working on adjusting the height of the parent class to fit the child class perfectly without overflowing Here is a snippet from my CSS file: Parent &__video position: relative; width: 471px; background: red; border-radius: 12px ...

Is there a way to remove the ring shadow in TailwindCSS?

Here is a visual representation of the issue I'm facing (refer to the image): Check out the Image After inspecting with Chrome, it seems like the problem is connected to --tw-ring-shadow. I attempted to use classes like ring-0 and ring-offset-0 (men ...

Send a triggering function to two separate components

My objective is as follows: render() { return ( <div> <Child onTrigger={xxx} /> <button onClick={xxx} /> </div> ) } Upon clicking the button, I wish for some action to take place in ...

SVG: organizing objects based on event priority

I am struggling with layering and event handling in an SVG element. Check out the example here: https://stackblitz.com/edit/angular-ivy-rkxuic?file=src/app/app.component.ts app.component.ts import { Component, VERSION } from '@angular/core'; @ ...

The MDBDataTable features header sections at both the top and bottom, but the filters UI seems to be

Currently, I am working with MDBDataTable and encountering an issue with the double heading that appears both on top and bottom of the table. I am unsure how to remove it. The code snippet in question is as follows: There is a function that retrieves and ...

Is it possible to adjust CSS values in a relative manner?

#bar{font-size:14px;} The font size of #bar is set to 14px. #foobar{font-size:$-2px} // Fictional code The font size of #foobar is now 12px. (calculated as 14px - 2px) Is there a way to dynamically alter the value of a selector like this? ...

Is there a way to navigate to the next or previous image in a lightbox by using ev.target.nextElementSibling or ev.target.prevElementSibling?

I am new to the world of web development Currently, I'm facing an issue with my lightbox feature. I want to navigate between images by using ev.target.nextElementSibling and ev.target.prevElementSibling when clicking on the next/previous arrow.png ic ...

How do I reference this span object using jQuery?

My HTML code includes the following: <span id="enterStreamName" style="display:none"> <input type="text" id='Stream' class="fullRowTextBox" value="" name="Stream" /> <button class="SaveStreamButton">Save</butto ...

A step-by-step guide on how to use the Google Closure Compiler: a

Is there anyone who could assist me in adding a snippet to the basic process of Google Closure Compiler? I have been trying unsuccessfully to do this via JavaScript code. I am using an example snippet from the official npm page, but when I run it, someth ...

`Is there a tutorial on parsing HTML with nested tags using Simple DOM Parser?`

I am currently working on parsing an HTML file that contains several DIV elements structured like this: <div class="doc-overview"> <h2>Description</h2> <div id="doc-description-container" class="" style="max-height: 605px;"> <di ...

What are some solutions for resolving a background image that fails to load?

HTML: `<div class="food-imagesM imagecontainer"> <!--Page info decoration etc.--> </div>` CSS: `.food-imagesM.imagecontainer{ background-image: url("/Images/Caribbean-food-Menu.jpg"); background-repeat: no-repeat; backgroun ...

Transfer PHP's uniqid() function to real-time using JavaScript with jQuery

Seeking a compact JavaScript(jQuery) code snippet to achieve this: date("r",hexdec(substr(uniqid(),0,8))); Your assistance is highly appreciated. Thank you. ...

PHP cannot recognize the data transmitted through Ajax requests

I am having trouble getting PHP to read a text sent via Ajax. index.html <head> <!--CSS Bootstrap--> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9cbc6c6d ...

Error: The function **now.toUTCString** is not recognized and cannot be executed by HTMLButtonElement

While the JavaScript code runs smoothly without setting a time zone, I encountered an error when trying to display the Barbados time zone. The issue is related to now.toUTCString function throwing the following error message. How can I resolve this? Uncau ...

Ways to implement the function provided in the website using javascript

Exploring the world of JavaScript functionality as a beginner, I am eager to try pasting dynamic data into HTML. I came across a helpful link providing instructions on how to achieve this using a table format. However, despite my best efforts, I am strugg ...

How can I implement two unique custom scrollbars on a single webpage?

I'm attempting to customize the scrollbar for a scrollable div separately from the scrollbar for the entire window. Despite my efforts, I haven't been successful in achieving two different colored scrollbars. *{ margin: 0; padding: 0; font-fam ...

Would this code effectively disable the right-clicking menu for MathJax?

My current approach involves utilizing the following code snippet: <script type="tet/x-mathjax-config"> MathJax.Hub.Config({ showMathMenu: false }); </script> I intended for this code to disable the right-click menu on my math webs ...

Shut down a pop-up overlay

I've been facing a challenge in implementing greasemonkey to automatically close a modal when the "x" button is clicked. Allow me to share with you the code snippet for the webpage: <div class="modal-header"> <button type="button" class="clo ...

Issue with page scrolling while utilizing the Wow.js jQuery plugin

While using the Wow.js plugin for scroll animations, I encountered an issue on mobile phones. When reaching the Pricings section, scrolling becomes impossible for some reason. I am utilizing Bootstrap 3 in this project. Despite attempting to modify the an ...

Utilize jQuery to extract various input/select box values and compile them into an array for submission using .ajax()

I am currently facing an issue with dynamically generated forms using PHP and updated with jQuery's .appendTo() function as visitors interact with it. My main goal is to collect all input text and select box values from the current form and submit the ...