I've recently improved the IssueTrackerProduct so that when you start to write in the little textarea it expands and grows vertically as the text gets larger and larger. Other sites like Highrise do this too for note taking.

Long story short, here's the demo and here's the solution:


function _getNoLines(element) {
  var hardlines = element.value.split('\n');
  var total = hardlines.length;
  for (var i=0, len=hardlines.length; i<len; i++) {
     total += Math.max(Math.round(hardlines[i].length / element.cols), 1) - 1;
  }
  return total;
}

$(function() {

  // First, for all the textareas that have lots of lines of text 
  // in them, we want to double their number of rows
  $('textarea.autoexpanding').each(function() {
     while (_getNoLines(this) > parseInt(this.rows))
       this.rows = '' + Math.round((parseInt(this.rows) * 1.5));
  });

  // When a user enters new lines, if they have entered more
  // lines than the textarea has rows, then double the textareas rows
  $('textarea.autoexpanding').bind('keyup', function() {
     if (_getNoLines(this) > parseInt(this.rows))
       this.rows = '' + Math.round((parseInt(this.rows) * 1.5));
  });

}

How you deploy this obviously depends on your context and application. The solution here depends on jQuery. The key solution was in realizing that the visual number of lines is a combination of the number of hard linebreaks and a ratio between the textarea's cols and the length of each hard line.

Here's an application with this in use that hopefully demonstrates the usefulness of this solution.

Comments

Your email will never ever be published.

Previous:
Nasty human error in Zope ZEO setup September 14, 2007 Zope
Next:
Ugliest site of the month - The Backyard Comedy Club September 21, 2007 Misc. links
Related by category:
How to SSG a Vite SPA April 26, 2025 JavaScript
Switching from Next.js to Vite + wouter July 28, 2023 JavaScript
An ideal pattern to combine React Router with TanStack Query November 18, 2024 JavaScript
get in JavaScript is the same as property in Python February 13, 2025 JavaScript
Related by keyword:
Command-Enter to submit a form when focus is inside textarea with React (TypeScript) February 4, 2022 React
Catching a carriage return in bash October 23, 2006 Linux
Changing the size of a textarea box August 18, 2004 Web development
To br / or not to br/ March 23, 2006 Web development