Adjust the width of a textarea dynamically as you type by detecting keyup events

Two text areas have the same text formatting. Their IDs are textareaA and textareaB.

I've successfully added functionality to resize textareaA on keyup. How can I make textareaB resize its WIDTH while the user is typing in textareaA?

Here's what I attempted, but it didn't work.

$(document).on("click blur keyup", ".fT", function() { // keyup event for textareaA
        var newWidth = $("#textareaA").val(); // this line does not achieve the desired effect
        $("#textareaB").width(newWidth);
});

Answer №1

To achieve this, use the JQuery method .css("property", "value").

Example :

$(document).keyup(function(event)) {
    $("#textareaB").css("width", "1000px");
});

Another example using a variable :

var myVariable = 1000; /* integer value here */

$(document).keyup(function(event)) {
    $("#textareaB").css("width", myVariable.toString() + "px");
});

Answer №2

 $('#TextAreaId').keyup(function(e) {
         while ($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth"))) {
             $(this).height($(this).height() + 1);
         };
     });

UPDATE 2: (Adjusting textarea width)

note ::modify the value '8' in the if statement based on your requirements and optimal fit

$(function() {   
$('#mta').keyup(function(e) {

    console.log($(this).outerWidth()+'--'+$(this).val().length*8);
         if($(this).outerWidth() > $(this).val().length) {
             $(this).width($(this).width() + 1);
         };
     });

 });

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

Prevent the risk of revealing your LinkedIn API key within HTML code

For my website, I am looking to incorporate the Sign In With LinkedIn feature for user logins. The initial example snippet provided in the LinkedIn API docs is as follows: <script type="text/javascript" src="//platform.linkedin.com/in.js"> api_k ...

Are we looking at a declaration of an arrow function? Is this concept even real?

I have been exploring the concept of function expressions versus function declarations with arrow functions. From my understanding, this is an example of an arrow function expression: const fred = greeting = () => { console.log("Hello from arrow ...

Tips for preventing unstyled content flash when updating CSS files dynamically

The following jQuery function is used to replace CSS files: function UpdateTheme(theme) { var links = $("link"); var _t = theme; $.each(links, function (i, link) { var _h = $(link).attr('href'); _updatedHr ...

Receiving an error while trying to install packages from the NPM registry due to non

I am facing some challenges while attempting to install my Ionic App through the registry along with its dependencies. I have been using npm i --loglevel verbose command, and my ~/.npmrc file is configured as follows: //nexus.OMMITED.com/repository/:_auth ...

Using Three.js to extract Vertex Colors based on the z-coordinate of Vectors

Here is a sample: http://jsfiddle.net/c3shonu7/1/ The code demonstrates the creation of a BufferGeometry object by cloning an IcosahedronBufferGeometry's vertices. The goal is to apply a color gradient to the subdivided icosahedron, with lighter shad ...

Is it possible to alter the value and label of an HTML button in a permanent manner?

I am developing a personalized management system that records your activities from the previous day based on the buttons you clicked. I have successfully implemented the ability to edit these activity buttons using jQuery, but I would like these changes to ...

An error with code -4058 occurred when I tried to execute the command npm run start-rewired

When I start my React application using npm with the command npm run start-rewired, I encountered an error that looks like this: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="265654494c434552661608170816">[email p ...

Unraveling the onsubmit FormObject in an HTML form within the Google Sheets Sidebar: Tips and tricks

In the sidebar, I am setting up a form with 27 inputs to create new sheets based on various criteria. The form includes a text entry field and multiple groups of radio buttons and checkboxes. However, I am currently facing an issue when trying to create a ...

Guide on displaying the appropriate child "div" with jQuery?

I am facing a challenge with my two dependent dropdowns that toggle the visibility of divs based on user input. The first div is functioning correctly, however, every time the user makes a selection in the second div, it impacts the first div. $(docume ...

Animating CSS styles in Vue.js based on JavaScript triggers

Within my Vue.js application, I've implemented a login form that I'd like to shake whenever a login attempt fails. Though I have achieved the desired shaking effect, I'm not completely satisfied with my current solution. Here's a simpl ...

Overlaying a div on top of an iframe

Struggling to make this work for a while now. It seems like a small issue related to CSS. The image isn't overlaying the IFrame at the top of the page as expected, it's going straight to the bottom. Here is the code snippet: .overlay{ width: ...

What is the reason behind the failure of this ajax request?

I have encountered an issue with my ajax code. It seems to work in one file, but fails to function properly in other files. Below is the code snippet in question: $.ajax({ type: "GET", url: "get_child.php?pc_id="+pc_id.toString(), }).done(fun ...

Regular expression for selecting characters and numbers in jQuery using a selector

Can someone help me figure out how to select all IDs like this: r1 r2 r3 etc. and not the other IDs like helloWorld I attempted using the CSS selector with jQuery but I'm having trouble understanding how. I tried $('#r[0-9]') but it didn& ...

Utilizing a ListView Below a LinearLayout: Step-by-Step Guide

In my Android project, I am working on creating the MyProfile activity. This activity will display a user's profile picture, name, and other bio data on the first screen. Additionally, I would like to make the layout swipeable so that when the screen ...

combine two JSON objects into a single object based on their unique identifiers

I am facing a challenge with two JSON objects that I have: Object 1: { "jcat":[ { "TITLE":"SEO", "ID":"27", }, { "TITLE":"Functions and Events", "ID":"2" }, ] } Object 2: { "job":[ { ...

When attempting to install an npm package from a local directory, I encountered a 404 Not Found error, despite the package existing in the node_modules directory

After installing an npm package from a local directory, I noticed that the package was successfully installed and is located in the node_modules directory. However, upon trying to access the package, I encountered the following error: 404 not found I a ...

Top techniques for dynamic loading of HTML and Javascript

I recently implemented a page that uses XHR requests to load a modal box containing a form. The form is comprised of HTML tags along with some JavaScript for validation and submission through another XHR request. Although the functionality works as intend ...

Troubleshooting: Vue.js custom select element not responding to blur event

Unique Scenario A custom autocomplete select component has been created that not only autocompletes but also allows for adding a new value if the result is not found. This functionality was discovered to be missing in the vue-select module. Explore the C ...

Troubleshooting: Why Your Long Polling Example Isn't Working in Just 5 Minutes

Can anyone spot any errors in this code? Check out the example here I attempted to replicate the example, but it's not functioning correctly. I am looking to create real-time notifications from a database. If anyone can provide tutorials or assistan ...

A regular expression in JavaScript for matching unpredictable numbers without any specific words preceding them

For instance, I am looking to extract the numbers that do not have 'the', 'abc', or 'he' before them; the89 (no match) thisis90 (match 90) iknow89999 (match 89999) getthe984754 (no match) abc58934(no match ...