Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
A pair of textareas to auto grow and shrink based on text entry into one

I've got two <textarea>s, one editable ta2 one is not ta1. They both need to expand as text is entered into the editable one, and shrink when text is removed.

    <div
      class="items-center p-2 align-top"
      v-if="isFirstColumn(index)"
    >
      <textarea class="resize-none" id="ta1" cols="30" rows="5" v-model="row.value" readonly="true" />
    </div>
    <span v-else>
      <textarea @input="autoGrow($event.target)" class="resize-none" id="ta2" cols="30" rows="5" v-model="row.value" />
    </span>

The event handler:

private autoGrow(element) {
  if ((element.scrollHeight)+'px' > (document.getElementById('ta1') as HTMLTextAreaElement).style.height) {
    element.style.height = 'auto';
    element.style.height = (element.scrollHeight)+'px';
    (document.getElementById('ta1') as HTMLTextAreaElement).style.height = 'auto';
    (document.getElementById('ta1') as HTMLTextAreaElement).style.height = (element.scrollHeight)+'px';
  }
  else{    
    (document.getElementById('ta1') as HTMLTextAreaElement).style.height = 'auto';
    (document.getElementById('ta1') as HTMLTextAreaElement).style.height = ((document.getElementById('ta1') as HTMLTextAreaElement).scrollHeight)+'px';
    element.style.height = (document.getElementById('ta1') as HTMLTextAreaElement).style.height;
  }
}

When entering more text into ta2, once ta2 surpasses ta1 in size they will both start expanding. However, when deleting text I have an issue: Despite there still being much more text in ta2, it will jump back to ta1's size every other deletion. This is because of setting them both to the same height and then backspacing. I cant figure out how to resolve this.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Source for autoresize: Creating a textarea with auto-resize

OPTION 2 (Pure JavaScript)

Simple (Add this JavaScript to your master script file and forget about it.)

const tx = document.getElementsByTagName("textarea");
for (let i = 0; i < tx.length; i++) {
  tx[i].setAttribute("style", "height:" + (tx[i].scrollHeight) + "px;overflow-y:hidden;");
  tx[i].addEventListener("input", OnInput, false);
}

function OnInput() {
  this.style.height = "auto";
  this.style.height = (this.scrollHeight) + "px";
}

Example code for you!

const tx = document.getElementsByTagName("textarea");
for (let i = 0; i < tx.length; i++) {
  tx[i].setAttribute("style", "height:" + (tx[i].scrollHeight) + "px;overflow-y:hidden;");
  tx[i].addEventListener("input", OnInput, false);
}

function OnInput() {
  this.style.height = "auto";
  this.style.height = (this.scrollHeight) + "px";
}
<div class="items-center p-2 align-top" v-if="isFirstColumn(index)">
      <textarea class="resize-none" id="ta1" cols="30" v-model="row.value" readonly="true"></textarea>
    </div>
    <span v-else>
      <textarea class="resize-none" id="ta2" cols="30" v-model="row.value"></textarea>
    </span>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!