Scroll bar in div that scrolls dynamically at the bottom

$('textarea').on('input', function() {
  var scrollHeight = parseInt($(this).prop('scrollHeight'));
  $(this).height(scrollHeight);
  $(this).prev('.Content').outerHeight(300 - $(this).outerHeight());
 
});

$(".Content").scrollTop($('.Content').height()) 
.OuterDiv
{
  width:200px;
  height:300px;
  border:1px solid #000;
  position:relative;
  }
.Content
{
  width:200px;
  max-height:250px;
  background-color:grey;
  overflow:auto;
  }
.text
{
  resize:none;
  position:absolute;
  bottom:0;
  left:0;
  width:194px;
  height:45px;
  max-height:145px;
  background-color:rgba(250, 120, 30,1);
  font-size:16px;
  font-family:Arial;
  overflow:auto;
  word-wrap:break-word;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="OuterDiv">
  <div class="Content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
  <textarea class="text" placeholder="Write some text..."></textarea>
</div>

Good morning,

Is there a way to keep the scroll bar at the bottom while typing in the textarea (the textarea expands upwards as text is added)? The scrollbar starts at the bottom after refreshing the page, but doesn't stay at the bottom when adding text.

Answer №1

I stumbled upon the solution in this helpful Stack Overflow post. Whenever you are typing, it's essential to always scroll back down to ensure you're at the bottom of the .Content section.

However, constantly scrolling down might be bothersome for users who intentionally scrolled up within the content area. To address this issue, it's advisable to incorporate an additional check.

$('textarea').on('input', function() {
  var scrollHeight = parseInt($(this).prop('scrollHeight'));
  $(this).height(scrollHeight);
  $(this).prev('.Content').outerHeight(300 - $(this).outerHeight());
  $('.Content').scrollTop($('.Content')[0].scrollHeight);
  
});

$('.Content').scrollTop($('.Content')[0].scrollHeight);
.OuterDiv
{
  width:200px;
  height:300px;
  border:1px solid #000;
  position:relative;
  }
.Content
{
  width:200px;
  max-height:250px;
  background-color:grey;
  overflow:auto;
  }
.text
{
  resize:none;
  position:absolute;
  bottom:0;
  left:0;
  width:194px;
  height:45px;
  max-height:145px;
  background-color:rgba(250, 120, 30,1);
  font-size:16px;
  font-family:Arial;
  overflow:auto;
  word-wrap:break-word;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="OuterDiv">
  <div class="Content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
  <textarea class="text" placeholder="Write some text..."></textarea>
</div>

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

Retrieve the JSON data corresponding to the file that was uploaded

While working on a modified version of the Jquery File Upload Plugin from Hayageek, I encountered an issue. The original code worked perfectly, but when I made modifications to insert the uploaded filename into a database, the JSON response did not include ...

Using percentages for CSS alignment and adjusting element widths

Greetings everyone! I have been struggling to align two divs - one on the right with a width of 30% and the other on the left with 70%. Despite my efforts, I have not been able to find a solution. I am hopeful that someone here can assist me. My setup inv ...

Finding the earliest and latest dates from an event list in Angular can be accomplished by sorting the dates and

Can someone help me find the earliest and latest date from the list of events? Below is the code snippet along with a screenshot of the console log message. events: Event[] = []; count=0; minDate = new Date(); maxDate = new Date(); constructor(pr ...

Can you tell if an array consists solely of empty objects in JavaScript?

When working with a list of objects, I often find myself wondering how to determine if the elements in the list are empty. It seems like it should be simple, but sometimes it can get confusing. For example, in my react function component: console.log(list[ ...

Displaying an image directly in front of the camera using Three.js

I am currently working on a flight simulator project and faced a challenge in placing a cockpit image in front of my camera controlled by FirstPersonControls. Despite trying various methods, I have not been successful in making it work. Here is what I hav ...

Adding an Ajax response to a div in HTML: A step-by-step guide

How can I add Ajax response to a div in my HTML code? Below is my ajax script: $(document).ready(function(){ $('.credit,.debit').change(function(){ var value=$(this).val(); $.ajax({ type:"POST", url:" ...

"Creating a function within a knockout viewmodel that is populated with JSON data: A step-by-step guide

Struggling with defining a function inside my viewmodel. I retrieve json data using jquery getJSON and then map it to the viewmodel. $.getJSON('/Company/GetCompanies', function(data) { var viewModel = new CompanyViewModel() viewModel.m ...

The menubar does not maintain a uniform appearance in Bootstrap 4

I am currently developing a navigation bar using Bootstrap 4. Here is the code I have so far: <div class="row mx-2"> <div class="dropdown"> <div class="btn btn-info dropdown-toggle" data-toggle='dropdown'> ...

How to fix the jquery error "Uncaught TypeError: Cannot read property 'style' of null" in an elegant way?

In my HTML code, I created a div element. When a user clicks a button, I run a simple JavaScript script to try to hide it: document.getElementById("samplques").style.display = "none"; However, I encounter an error when I execute the script: cannot rea ...

Introducing the new default in Google Chrome: Now accessing window.opener for target="_blank" links after rel="noopener" is the standard feature

Exploring a new attribute called rel="noopener", I discovered that it keeps opened windows unaware of their opener. After some experimentation, I consistently found window.opener === null, only to realize that this is now the default behavior in ...

How can you generate a "Package Contains Lower Node Version" error message during the installation of an NPM package if the node version is higher than the current system's node version?

I am looking for a way to trigger an error during the installation of an NPM package if the node version supported by that module does not match the system/server node version. Specifically, I want to prevent the installation of any npm module that suppor ...

Using jQuery's ajax method with the querySelector.value property

I'm new to coding and I have a question that I hope is simple to answer. I'm trying to send an ajax request with data from two radio buttons. Here is the function I have: pOk = function () { var p1 = document.querySelector('input[name = " ...

JavaScript and jQuery - Struggling to retrieve checkbox values

In my dynamically generated (ASP) photo gallery, I have multiple checkboxes. Each checkbox is labeled as 'photos' and contains a unique photo ID in its value attribute. Here's an example of how the checkboxes are structured: <form name=" ...

How can I adjust the appearance of an HTML tag within an Angular component?

I have created an Angular component with the following definition: import { Component, Input, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'rdc-dynamic-icon-button', templateUrl: './dynamic-icon-button. ...

Clicking on a row in the Gridview should trigger the expansion or collapse of the Nested

My current issue involves a nested GridView setup like this: <asp:GridView ID="gvParent" runat="server" DataKeyNames="id"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:GridView ID="gv ...

What is the best way to generate a tilted slant with text positioned next to it?

.image { min-height: 100vh; } .bg-image { background-image: url('https://picsum.photos/1440/900'); background-size: cover; background-position: center; } <!--About Us --> <div class="container-fluid"> <div class="row no- ...

Is it possible for JavaScript to interact with elements that are created dynamically?

It's puzzling why the newly generated elements remain unseen by the next function that is called. Any insights on how to address this issue would be greatly appreciated! Resolve: Incorporate async: false to deactivate the asynchronous feature in order ...

Exploring search results using dynamic multiple dropdown lists in PHP

Be sure to check out this interesting link: There are six drop down boxes available on the page. These include options for STATE, DISTRICT, LOCATION, MEDICINE, SPECIALTY, and GRADE. I successfully retrieved all the necessary data from the SQL database to ...

Is there a way to display a floating image in Rmarkdown HTML output?

Looking for a way to have my company's logo floating at the top of an rmarkdown file with html output. Is there a method similar to the floating table of contents in rmarkdown? ...

What is the best way to use a timeout function to swap div layers in AngularJS?

Greetings, esteemed members of the Angular Technorati. I bring forth a perplexing yet seemingly simple issue that requires your expertise. My goal is to dynamically switch out a div layer after approximately 11 seconds and display another div layer. How ca ...