It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum Script-archive

Autosizing TextAreas with JavaScript

Autosizing TextAreas with JavaScript

Postby HotNoob on Fri Sep 24, 2010 5:00 pm

Code: Select all
textAreaMin = 3;
function sizeTextAreaHeight(obj)
{
  var text = obj.value.replace(/\s+$/g,"")
  var split = text.split("\n")
  var count = split.length; 
  if(count > textAreaMin)
  {
      obj.rows = count + 1;
  }
}
function sizeTextAreaWidth(obj)
{
    var parent = obj.parentNode;
    var width = (parent.offsetWidth == 0)?parent.clientWidth:parent.offsetWidth;
    obj.cols = Math.floor(0.123 * width);
   
}

Since figuring out how many columns and rows to use can be difficult, since its not in px, i've created some functions to help.

The sizeTextAreaHeight determines the number of rows to use based upon the content inside of it

And the sizeTextAreaWidth determines the number of columns based upon the width of the parent object that it is in. For example, if the parent object is a div with a width of 200px, the textarea will become roughly 200px wide.
HotNoob
100+ Club
 
Posts: 169
Joined: Sun May 02, 2010 1:38 am

Who is online

Users browsing this forum: No registered users and 0 guests