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

167
Views
Textarea auto-resize to fit content when resizing browser window

I want to create a textarea that expands or contracts to fit the content as the browser window is resized. So when the browser window width increases, the height of the textarea should decrease so that it is as tall as the content; when the browswer window width decreases, the height of the textarea should increase to show all of the content. Here's what I have now, with reference to textareas that expand with content input:

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

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

How do I get this to work?
*Edit: clarifying what I mean by auto-resize
*Edit 2: I checked related questions but they all address how textareas resize with user input, whereas I'm asking how they resize when the browser window is resized. I'm new to Javascript so I don't know how to apply these solutions to my problem.

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

0

If you want the textarea to expand depending on browser window, u can use viewport units.

Vh stands for 1% of the viewport height and vw for 1% of the viewport width.

100vh //100% of the viewport height
100vw //100% of the viewport width
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!