Expandable or Auto-Resize TextBox Height

Recently, I was asked to prepare a Facebook like expandable textbox in a ASP.NET web application, where user can input text and the textbox can be re-sized (more specifically the height) dynamically.

I searched for a while and found quite a lot of solutions that use jQuery (example). However, I was looking for something more intrinsic and simple, and finally I found another script on a forum that just use few lines of JavaScript codes:

1. Control declaration:

<asp:TextBox ID="txtMsg" runat="server"  TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this, event)" Rows="2" />

2. JavaScript function:

function AutoExpand(txtBox, event)
{
    if (event.keyCode == "13" || event.keyCode == "8") {
        var therows = 0
        var thetext = document.getElementById(txtBox.id).value;
        var newtext = thetext.split("\n");
        therows += newtext.length

        document.getElementById(txtBox.id).rows = therows;
        return false;
    }
}

That's it. Just put the script above in a script file and then re-use it for any buttons in the whole application.

Published Sunday, May 30, 2010 5:25 PM by Colt
Filed under:

Comments

# re: Expandable or Auto-Resize TextBox Height

ghfhfffh

ghfh

gh

hghf

gfh

fhg

fh

f

hg

fh

gfh

h

gfh

g

h

Thursday, July 29, 2010 2:53 AM by gfhhg

# re: Expandable or Auto-Resize TextBox Height

perfect!!!!!!!!!!!

Saturday, August 07, 2010 6:10 AM by Rainmaker

# re: Expandable or Auto-Resize TextBox Height

This is great. Helped me with my text box. I wonder how to increase the height and width of the default text box?

Thanks!

Friday, September 24, 2010 12:39 PM by Mark

# re: Expandable or Auto-Resize TextBox Height

Many Thanks....

Monday, November 01, 2010 9:52 AM by rajputpankaj

# re: Expandable or Auto-Resize TextBox Height

asdf

asdf

asdf

asdf

asdf

asdf

asdf

asdf

asdf

asdf

asdf

asdf

asdf

Friday, January 07, 2011 4:04 AM by Pjotr

# re: Expandable or Auto-Resize TextBox Height

Hey! Thanks. I was searching for this answer for last couple of months and finally it worked for me now. Thanks again.

Wednesday, January 12, 2011 4:19 AM by Biggi Mark

# re: Expandable or Auto-Resize TextBox Height

this is excellent; only the problem is user have to hit enter button, what i want is  when user keep typing and it reaches to the text area width it automatically goes to next row then the text box should also resize depending upon if it reached last row

any idea on this?

Sunday, January 16, 2011 6:59 AM by nizam133

# re: Expandable or Auto-Resize TextBox Height

Here's my suggestion:

First, declare the textarea (no need for a server-side one) and tag with a class (eg "vertical_resize")

<textarea class="vertical_resize"></textarea>

Now bind the keyup eventto a simpler version of the above function, using jQuery:

$('textarea.vertical_resize').bind({

   keyup: function() {

       this.rows = this.value.split("\n").length;

   }

});

Until now, nothing's new (except the fact that the textarea's size is calculated on each key).

However, this causes a bad effect where the text first updates and some milliseconds later the textarea resizes. It causes the text to 'jump' a little before updating.

In-order to fix this, we can bind to the 'keydown' event (which triggers before the text is updated) and add one row when the user presses 'enter':

   keydown: function(event) {

       if (event.keyCode == "13")

       {

           this.rows += 1;

       }

   }

The whole bind looks like this:

$('textarea.vertical_resize').bind({

   keyup: function() {

       this.rows = this.value.split("\n").length;

   },

   keydown: function(event) {

       if (event.keyCode == "13")

       {

           this.rows += 1;

       }

   }

});

Wednesday, January 19, 2011 4:36 AM by Adrian Aisemberg

# re: Expandable or Auto-Resize TextBox Height

Update to my previous suggestion:

The user can still paste some text using the mouse.

Binding to the 'paste' event and triggering the resize after 50 milliseconds does the trick:

...[previous bind]...

,

paste: function() {

   var textarea = this; // we have to save the textarea because 'this' won't point to it inside the timer's callback.

   setTimeout(function()

       {

           textarea.rows = textarea.value.split("\n").length;

       }, 50);

}

There's a little code-duplication (the ...rows=...split... thing), so we better move the line to a function and call it instead:

function resizeTextArea(textarea) {

   textarea.rows = textarea.value.split("\n").length;

}

Wednesday, January 19, 2011 5:05 AM by Adrian Aisemberg

# re: Expandable or Auto-Resize TextBox Height

Expandable or auto resize textbox height.. Awful :)

Thursday, March 31, 2011 11:13 AM by weblogs.asp.net

# re: Expandable or Auto-Resize TextBox Height

Expandable or auto resize textbox height.. Very nice :)

Wednesday, April 27, 2011 3:56 PM by weblogs.asp.net

# re: Expandable or Auto-Resize TextBox Height

Expandable or auto resize textbox height.. OMG! :)

Friday, June 10, 2011 3:34 PM by weblogs.asp.net

# re: Expandable or Auto-Resize TextBox Height

czxz

zxcz

xcxz

c

xzc

xzc

xzc

xz

cxz

cxzc

xzcxz

cxz

cxz

cxz

cxz

cxz

cxzcxzc

xzc

xzcxzc

Tuesday, August 30, 2011 11:29 AM by zczxczx

# re: Expandable or Auto-Resize TextBox Height

zxcxz

zx

c

zxc

zx

c

zx

czx

c

zx

c

xzc

zx

c

zx

c

zx

Saturday, September 03, 2011 6:19 AM by zxczxczx

# re: Expandable or Auto-Resize TextBox Height

FTERTER RMERT ndjasdsadsabdj sh gdhsgadsagjdgsa sagdhysagdhygstyad shdgtysafdtyfsatydftyfstad sgfd tysafdtyfta safdysagdty ffsatayd fsatdftsafdrt fsatysfdtysadtbdjsa sabdhahfjehf eehrwegrugwer euigreuwgruguwegrunjklsbfhsa shdhasfsgf shhsgafgsafgd sgfgsafgsga sgadgsajgfusaf  jty jhyj  hjihirth gjhklgjhjgkl gkjhogjojh g hioghfgihifg gjh ifghiohiog jhighigio gijhighjfgih gijhojfophifg jgihojfgihj ifgjhifg fgihj gifhjifgj ifgj igf jhghjfgihjghigfjh rtyrtytrytr

Tuesday, October 04, 2011 3:01 AM by Swa

# re: Expandable or Auto-Resize TextBox Height

How about expandable table rows ?

Thanks,

Friday, October 14, 2011 3:35 PM by Nicole

# re: Expandable or Auto-Resize TextBox Height

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

Tuesday, April 17, 2012 7:45 AM by test

Leave a Comment

(required) 
(required) 
(optional)
(required)