How can I make a textarea automatically expand using jQuery?
I have a textbox for explaining the agenda of the meeting, so I want to expand that textbox when my agenda's text keep growing that textbox area.
How can I make a textarea automatically expand using jQuery?
I have a textbox for explaining the agenda of the meeting, so I want to expand that textbox when my agenda's text keep growing that textbox area.
People seem to have very over worked solutions...
This is how I do it:
This will work with long lines, as well as line breaks.. grows and shrinks..
(This will not work in Internet Explorer 9 or older as it makes use of the
input
event)You can try this one
I just built this function to expand textareas on pageload. Just change
each
tokeyup
and it will occur when the textarea is typed in.Tested in Chrome, IE9 and Firefox. Unfortunately Firefox has this bug which returns the incorrect value for
scrollHeight
, so the above code contains a (hacky) workaround for it.Old question but you could do something like this:
html:
jquery:
This way the auto resize will apply to any textarea with the class "text-area". Also shrinks when text is removed.
jsfiddle:
https://jsfiddle.net/rotaercz/46rhcqyn/
I have tried lots and this one is great.Link is dead. Newer version is available here. See below for old version.You can try by pressing and hold enter key in textarea. Compare the effect with the other auto expanding textarea plugin....
edit based on comment
note: you should include the needed js files...
To prevent the scrollbar in the textarea from flashing on & off during expansion/contraction, you can set the
overflow
tohidden
as well:Update:
The link above is broken. But you can still get the javascript files here.