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 7, 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 1, 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 7, 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

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 3, 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 4, 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

# re: Expandable or Auto-Resize TextBox Height

frsdjklfjsdlkfjsdlfsdfsdf

sdf

sdf

sd

f

sd

fsd

f

sd

f

sdf

sd

f

sdfsd

fs

fsd

fsd

f

sd

fsssssdf

sdfsd

fsd

f

sd

f

sdf

sd

f

sd

f

sd

fsd

f

sd

f

sd

f

Monday, July 16, 2012 3:21 AM by oned_gk

# re: Expandable or Auto-Resize TextBox Height

OMG...so easy. Love your work.

Friday, August 31, 2012 4:05 AM by Bugalugs

# re: Expandable or Auto-Resize TextBox Height

fsdf

sdf

Saturday, September 1, 2012 11:54 AM by sdf

# re: Expandable or Auto-Resize TextBox Height

r2r

r23r

r23r2

r2r2

r2r

Friday, September 28, 2012 1:42 AM by r32r2