Adjusting the size of a DIV based on the number of characters entered in the text field

I have created a text box that dynamically prints out the entered text in a div below as it is typed. The current setup allows for 24 characters to be displayed in the DIV before text wrapping occurs. My goal is to double the height of the DIV for every additional 24 characters entered.

I am aiming to achieve this functionality using only pure JavaScript, without relying on jQuery

<div class="one">Input Some Text
        <form>
            <input type="text" id="pointlessInput"/>
            <script>
                var stringToBePrinted = document.getElementById("pointlessInput");
                var len = stringToBePrinted.length;

                stringToBePrinted.onkeyup = function(){
                    var len = stringToBePrinted.length;
                    document.getElementById("printbox").innerHTML = stringToBePrinted.value;
                    if(document.getElementById("pointlessInput").innerHTML.value.length == 24){
                        document.getElementById("printbox").style.height = "4em";
                    }
                }

        </script>
    </form>
    <div class="printbox" id="printbox"></div>
</div>

stylesheet

.printbox {
border-width:thick 10px;
border-style: solid; 
background-color:#fff;
line-height: 2;
color:#6E6A6B;
font-size: 14pt;
text-align:center;
border: 3px solid #969293;
width:50%;
height:2em;
margin: auto;
word-wrap: break-word;
}

Answer №1

const textToDisplay = document.getElementById("irrelevantContent");

textToDisplay.onkeyup = function() {
  document.getElementById("displayArea").innerHTML = textToDisplay.value;
  let lines = Math.ceil(parseInt(document.getElementById("irrelevantContent").value.length) / 24);
  document.getElementById("displayArea").style.height = (lines * 2) + "em";   
}

Answer №2

Try removing the if condition and the height from the CSS, it should work without any issues. Take a look at the example below:

var textToDisplay = document.getElementById("pointlessInput");
var length = textToDisplay.length;

textToDisplay.onkeyup = function(){
var length = textToBePrinted.length;
document.getElementById("printbox").innerHTML = textToBePrinted.value;
}
.printbox {
border-width:thick 10px;
border-style: solid; 
background-color:#fff;
line-height: 2;

color:#6E6A6B;
font-size: 14pt;
text-align:center;
border: 3px solid #969293;
width:50%;
margin: auto;
word-wrap: break-word;
}
<div class="one">Input Some Text
        <form>
            <input type="text" id="pointlessInput"/>
            <script>
                
            </script>
        </form>
        <div class="printbox" id="printbox"></div>
    </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

Request for a new login using credentials via ajax

We've set up a web application on a LAMP(PHP) system that makes around 40 Ajax calls. Users are tracked using PHPSessions with a 1 hour cookie lifespan (which we want to maintain for quick expiration). ISSUE: If a user is inactive for the full hour an ...

Ways to retrieve the value of an Object that may have a key that is undefined

Is there a method similar to Angular's $parse service that can be used for accessing nested object properties? Consider an object structure like this: const obj = { items: [ { store: { type: '' } } ] }; Sce ...

What causes addEventListener to not return a value?

In this snippet of code: let rockClick = rockBtn.addEventListener('click', playRound.bind("rock", computerPlay(), false)); After using console.log(), the output is undefined. The purpose of rockBtn:const rockBtn = document.querySelecto ...

programming / mathematics - incorrect circular item rotation

I need help arranging a sequence of planes in a circular formation, all facing toward the center. However, I seem to be experiencing issues with the rotation after crossing the 180-degree mark. While my objects are correctly positioned around the circle, t ...

Rapidly update code changes using the development mode in Next.js with the VS Code Remote Container/devcontainer

Struggling to enable Next.js' Fast Refresh feature while using a VS Code Remote Container. Running npm run dev displays the app on localhost, indicating the container functions properly - but Fast Refresh remains ineffective. Next.js version: v11.0.1 ...

Craft a stunning transparent border effect using CSS

I am trying to achieve a unique border effect for an element using CSS, where the transparency is maintained against the background: This is the desired outcome: https://i.stack.imgur.com/6Z1MU.png However, the border I am currently able to create looks ...

Fill a popup window with data retrieved from a MySQL query result

I have created an HTML page that displays data retrieved from a MySQL database. Users have the option to edit records, and I want to implement a modal form that opens up and shows data related to a specific "id" from the database when the edit button is cl ...

What is preventing the onmouseup event from being effective?

In my recent research, I came across some interesting information on mouse-click events from w3schools. According to them, the onmousedown, onmouseup, and onclick events are all crucial parts of a mouse-click process. It starts with the onmousedown event b ...

"Learn the technique of adding a comma after each object and transforming it into an array using JavaScript in React JS

Is there a way to add commas after each object and transform it into an array? Below is the response data: {"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42& ...

Is there a way to use jQuery to make a div fade in and toggle over another

I have created a test page and I am looking to achieve a specific effect when the page loads. I want everything on the page to be hidden initially. When I click on the "About" text, I want it to fade in using fadeToggle(); however, when I click on "My work ...

After successfully posting a tweet, I have not heard back from Twitter

My objective is to achieve the following on click: Share the URL and text on Twitter in a popup window Receive a response from Twitter indicating whether the tweet was successful or failed Close the popup window after the tweet is successfully pos ...

Reorganize items that extend beyond the list into the next column using Bootstrap

I have a row with two columns, where Column 1 currently contains 7 list items under the ul tag. However, I want to display only 5 list items in Column 1 and automatically move the remaining items to the next column (i.e., Column 2). Is there a way to ach ...

Adjusting column sizes smaller than 1/12 within the Primeng flex grid in Angular 8

In using primeng grid, a common desire is to have 2 columns behave a certain way: the first column should take up 1/13 of the total space the second column should take up the remaining space. Many attempt different solutions like: <div class="p-g ...

In the template, I utilize both vue-router and jQuery. However, there seems to be an issue with $(document).ready() not functioning

Looking for guidance on using jQuery in Vue. I've noticed that when I refresh the page, $(document).ready() function is triggered. However, when I use vue-router to switch pages, $(document).ready() does not seem to work. <template> <div ...

Swap out the current image for a different one

When a user clicks on different image or color options, I want to change the main image displayed. Below are the links to the alternative images: https://i.sstatic.net/DxJEb.jpg This is the HTML code: <div class="container"> <p class="img-main" ...

How to implement HTML5 Application Cache in an ASP.NET MVC4 project to enable offline functionality for a web application?

Exploring the concept of app caching in HTML5 with static content and file extensions such as index.html, theme.css, and app.js. When it comes to using MVC4 for a web app, how can we cache dynamic data obtained from an API and stored in localStorage? I a ...

The problem of creating a custom pop-up with jQuery

I'm running into an issue while trying to develop a custom CSS and jQuery popup. Despite my efforts, the popup isn't functioning as intended. Take a look at my code below and point out where I may have gone wrong. What I aim to achieve is a popup ...

No wrapping of columns in Bootstrap 4

I am struggling with preventing the B4 columns from wrapping when resizing the Browser. Check out my code below: <div class="card card-outline-primary"> <div class="card-block"> <form class="row"> <div class="col-xs-4" sty ...

Pulling data from a MySQL database using AJAX and PHP to convert it into a JavaScript variable, resulting in

I am attempting to retrieve MySQL data using PHP, convert it to JSON, and then pass it into my JS variables for Chart.js. The JSON generated by PHP appears correct. However, when trying to access the data within my variables, the console is displaying them ...

Position-Scrolling Attribute

While exploring this angularJS app example (), I came across an attribute named scroll-position in the following div tag that is set to "scrolled": <div class="page-top clearfix" scroll-position="scrolled" max- height="50" ng-class="{'scrolled&ap ...