Conversation panel text slipping out of <div>

I am currently working on creating a mock "chat" feature. The issue I am facing is that when I repeatedly click the "send" button, the text overflows from the div. Is there a way to prevent this by stopping the text near the border of the div or adding a scrolling feature? I can only use HTML, JavaScript, and CSS.

function postChat(){
var node = document.createElement("p");
var content= document.getElementById("comment").value;
var comment= content;
var textNode = document.createTextNode(comment);
node.appendChild(textNode);
document.getElementById("allComment").appendChild(node);
}
#chatbox {
width:50%;
text-align:left;
background-color:#F7F7F7;
height:250px;
border:1px solid black;
}
<body>
<div id="chatbox">
<div id="allComment" style="font-size:10px; line-height:90%;" ></div>
</div>
<p>
<input type="text" id="comment" />
</p>
<input type="button" value="Send" onclick="postChat()" />
</body>

Answer №1

Apply the CSS property overflow:auto

function postchat(){
var node =document.createElement("p");
var content= document.getElementById("comment").value;
var comment= content;
var textnode = document.createTextNode(comment);
node.appendChild(textnode);
document.getElementById("allComment").appendChild(node);
}
#chatbox {
width:50%;
    text-align:left;
    background-color:#F7F7F7;
    height:250px;
    border:1px solid black;
    overflow: auto;
}
<body>
<div id="chatbox">
<div id="allComment" style="font-size:10px; line-height:90%;" ></div>
</div>
<p>
<input type="text" id="comment"></input>
</p>
<input type="button" value="Send" onclick="postchat()" />
</body>

Answer №2

To create scrollbars, use the following CSS code: overflow-y: scroll;

I am having trouble reproducing the issue you described with multiple clicks on send – can you provide more details?


function postchat(){
var node =document.createElement("p");
var content= document.getElementById("comment").value;
var comment= content;
var textnode = document.createTextNode(comment);
node.appendChild(textnode);
document.getElementById("allComment").appendChild(node);
}

#chatbox {
width:50%;
text-align:left;
background-color:#F7F7F7;
height:250px;
border:1px solid black;
overflow-y: scroll;
}

<body>
<div id="chatbox">
<div id="allComment" style="font-size:10px; line-height:90%;" ></div>
</div>
<p>
<input type="text" id="comment"></input>
</p>
<input type="button" value="Send" onclick="postchat()" />
</body>

Answer №3

Make sure to add the CSS property overflow:auto to prevent the content of the p tag from overflowing its container.

#chatbox {
    width:50%;
    text-align:left;
    background-color:#F7F7F7;
    height:250px;
    border:1px solid black;
    overflow: auto;
}

Answer №4

Ensure that you add overflow: auto; to the CSS of #chatbox. This will trigger a scroll bar within your #chatbox when the text exceeds the div's content.

For more detailed information on overflow, check out this resource.

overflow: auto; will automatically handle overflow for both the horizontal (X) and vertical (Y) axis.

function postchat(){
    var node = document.createElement("p");
    var content = document.getElementById("comment").value;
    var comment = content;
    var textnode = document.createTextNode(comment);
    node.appendChild(textnode);
    document.getElementById("allComment").appendChild(node);
}
#chatbox {
    width: 50%;
    text-align: left;
    background-color: #F7F7F7;
    height: 250px;
    border: 1px solid black;
    overflow: auto;
}
<body>
<div id="chatbox">
<div id="allComment" style="font-size: 10px; line-height: 90%;" ></div>
</div>
<p>
<input type="text" id="comment"></input>
</p>
<input type="button" value="Send" onclick="postchat()" />
</body>

Answer №5

If you use either overflow:auto or overflow-y:scroll, it should solve your scrolling needs.

overflow:auto allows scrolling in all directions when necessary, while overflow-y:scroll keeps the scrolling limited to the vertical (y) axis only.

#chatbox {
width:50%;
    text-align:left;
    background-color:#F7F7F7;
    height:250px;
    border:1px solid black;
    overflow-y: scroll;
    /*  overflow: 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

Trigger a function when the value of the file input field changes using ng-change

I'm attempting to trigger my upload() function when the file input changes, but I'm having trouble getting it to work. This is the HTML code: <input type="file" ng-model="image" ng-change="uploadImage()"> And here's the correspondin ...

On mobile devices, the height of the absolutely positioned element is larger than its

I currently have an element with a background image that includes an absolutely positioned element at the bottom. As the screen width decreases, the height of the absolutely positioned element increases and surpasses the parent element. Since absolutely p ...

Implementing Module Dependencies using RequireJS

I'm currently working with RequireJS and encountering some issues when trying to require local libraries. In my process, I've set up a JS file to import the library that was installed using npm JS function synthesizeToAudioFile() { ...

Creating clickable div elements within a loop

In the .JS file I have the function below (which I later call in an HTML file): function writeDiv(){ x = 1 myArray.forEach(item => { htmlText += '<div class="divsArray">'; htmlText += '<h5> Value of X ...

Unlocking the request object within a GraphQL resolver with Apollo-Server-Express

My express server is standard and I'm using GraphQL with it const server = express(); server.use('/graphql', bodyParser.json(), graphqlExpress({ schema })); I am wondering how to access the request object within a resolver. Specifically, ...

Bringing the Jquery cursor into focus following the addition of an emoji

Looking to add emojis in a contenteditable div but facing an issue with the cursor placement. Here is a DEMO created on codepen.io. The demo includes a tree emoji example where clicking on the emoji adds it to the #text contenteditable div. However, after ...

Enhancing this testimonial slider with captivating animations

I have designed a testimonial slider with CSS3 and now I am looking to enhance it by adding some animation using Jquery. However, I am not sure how to integrate Jquery with this slider or which plugins would work best for this purpose. Can anyone provide g ...

Steps to enable overflow: 'scroll' for components generated dynamically

When developing a React application, I encounter an issue with creating new components dynamically inside a container. The problem arises when the height of the container gets exceeded by the newly added items, and I aim to make the container scrollable in ...

Learn how to implement JavaScript code that allows a video to start playing only upon clicking a specific button

Within the confines of a div lies a <video autoplay> <source src='myvid'> </video>. This div initially has a display ='none' setting in its CSS. Upon receiving a click event, the display property changes from none to b ...

The Express API is failing to recognize the data keys that were sent from the React frontend, despite being clearly specified

I am facing an issue while trying to send data to a REST API using React hosted in a separate application. The API does not seem to receive the keys sent, even though I checked the results in Chrome and found this output:(2) ["imageSrc", File]0: "imageSrc" ...

Fresh ajax requests are not clearing the current data displayed in the JSP table

My ajax function retrieves data from a servlet and displays it in the page successfully. However, each time a new ajax call is made, the form appends the new data to the existing results instead of replacing them. I need to reset the current values stored ...

Decrease in internal width

Seeking assistance to adjust the internal border width for better text alignment. I've been struggling with this issue as a beginner in the web development field and would appreciate some guidance. Link to image Here is a snippet of my HTML code: & ...

A Beginner's Guide to Duplicating Bootstrap Containers in Jade

I am working with JSON data that is being transmitted from an Express and Mongoose Stack to be displayed on the user interface created in Jade. I am wondering which Jade Construct I should use to loop through a Bootstrap Container of col-md-4 using Jade s ...

Unusual actions observed in ajax rails form calculator with pre-filled values

I have a form calculator in Rails that utilizes AJAX without a database. While it functions properly, I have noticed some peculiar behavior which I would like to address. Firstly, I navigate to the /interest_calculator page Then, I input 10 into @first_0 ...

Disable the parent CSS styling when focusing on the child button

Within an anchor tag, I have a div element. The CSS on the :active state is set to transform the element, but I want to avoid this behavior when focusing on the child element bx--overflow-menu. I am working with NextJS using styled-jsx and SaSS. Here is my ...

What is the best way to remove the hover effect from a specific element within a div?

I am looking to achieve a specific hover effect where the white part does not darken when hovering over a certain element within its child elements. Here is the HTML code I have: <div className= {css.searchBarDiv}> <div className={css.searchBar ...

How can I show a tooltip in vuetify when a button is disabled?

I am currently using the button and tooltip components in my Vuetify project. I am trying to find a way to only display the tooltip when the button is disabled, but I'm having trouble figuring out how to do it correctly. Right now, the tooltip only a ...

Don't allow users to switch views without saving their changes

We are working with a Backbone.js application that presents various forms to users. Our goal is simple: if a user navigates away from the page without saving the completed form, we need to show a confirmation dialog. When dealing with traditional forms, i ...

Update information interactively in Vuejs for all stored data

I am facing an issue with my code where the totalUserPosts value is returning as Zero after an AJAX call, and I need to find a solution for this problem. new Vue({ el: "#app", data: { totalUserPosts:0, }, methods:{ getPostsAjax () { $.ajax({ ...

Two interactive dropdown menus featuring custom data attributes, each influencing the options available in the other

Looking to create interactive select boxes based on the data attribute values? This is the code I currently have: HTML <select id="hours" onchange="giveSelection()"> <option value="somethingA" data-option="1">optionA</option> <o ...