I have found various plugins for auto-growing a textarea, but not input text fields. Does anybody know if any exist?
-
Just to clarify: Are you looking for someone where when the text is about to become too big for the textfield, the field keeps expanding to show all the text at once? – Paolo Bergantino May 31 '09 at 04:11
10 Answers
Here's a plugin that'll do what you're after:
EDIT: I've fixed the plugin as per Mathias' comment. :)
See a demo here: http://jsfiddle.net/rRHzY
The plugin:
(function($){
$.fn.autoGrowInput = function(o) {
o = $.extend({
maxWidth: 1000,
minWidth: 0,
comfortZone: 70
}, o);
this.filter('input:text').each(function(){
var minWidth = o.minWidth || $(this).width(),
val = '',
input = $(this),
testSubject = $('<tester/>').css({
position: 'absolute',
top: -9999,
left: -9999,
width: 'auto',
fontSize: input.css('fontSize'),
fontFamily: input.css('fontFamily'),
fontWeight: input.css('fontWeight'),
letterSpacing: input.css('letterSpacing'),
whiteSpace: 'nowrap'
}),
check = function() {
if (val === (val = input.val())) {return;}
// Enter new content into testSubject
var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(/</g, '<').replace(/>/g, '>');
testSubject.html(escaped);
// Calculate new width + whether to change
var testerWidth = testSubject.width(),
newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth,
currentWidth = input.width(),
isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth)
|| (newWidth > minWidth && newWidth < o.maxWidth);
// Animate width
if (isValidWidthChange) {
input.width(newWidth);
}
};
testSubject.insertAfter(input);
$(this).bind('keyup keydown blur update', check);
});
return this;
};
})(jQuery);
-
2Awesome plugin, James! One suggestion though: perhaps it would be cool if the INPUTs would also decrease in size when text is removed. – Mathias Bynens May 31 '09 at 11:09
-
1
-
1
-
13Great! This is totally what I was looking for, you should definitely blog about this and get some link bait around this so others can find it in Google. – Brad Gessler Jun 08 '09 at 17:46
-
Is there a reason why you use `var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(//g, '>');testSubject.html(escaped);` instead of `testSubject.text(val)` – jantimon Jan 21 '10 at 10:17
-
1wicked script, James! Don't know if I'll use it, but it's been a while since I last felt that surge of joy that nice javascript creates! – Morten Bergfall Jan 24 '10 at 11:46
-
-
1Why not create a text node containing the input value and use CSS `white-space: pre` on your `testSubject` element rather than escaping everything? And why make up an invalid element name ("tester") rather than use a simple `` or ``?– Tim Down Mar 12 '12 at 14:54
-
I've made an example: http://jsfiddle.net/rRHzY/ can anyone tell me why doesn't this work as expected in FIREFOX? In FireFox when I start to type spaces in the input field I'm getting closer and closer to the end, and at a certain point the input becomes "too small" – Gavriel Apr 16 '12 at 11:52
-
BTW I've put the code here for everyone's reference and contributions: https://gist.github.com/2843893/c33972ea0f47cb45c250d4d2dcfb13ae10f89003 and I've made one change so far which is to set the field to max width if the new width is over the max width. Used to just not change the field at all if over max width. Thanks again for the very useful code. – Matt Hall May 31 '12 at 14:50
-
To have the input snap to placeholder text when there is no value, change `if (val === (val = input.val())) {return;}` to `var input_val = input.val(); if ( input.attr('placeholder') && !input.val() ) input_val = input.attr('placeholder'); if (val === (val = input_val)) {return;}` – Andrew Tibbetts Feb 28 '13 at 17:31
-
To have the input snap to content that has been loaded via ajax, put `.trigger('update')` at the end of the content placement. – Andrew Tibbetts Feb 28 '13 at 22:09
-
Nice! One fix proposal (extremely common bug): update width when user pastes text in (using context menu). It doesn't update, at least not in Chrome. – frnhr Mar 25 '13 at 19:13
-
If you want to combine this plugin with jQuery UI autocomplete, update the bind line to include the autocompleteclose event (otherwise a mouse selection from the menu won't trigger an update): $(this).bind('keyup keydown blur update autocompleteclose', check); – redreinard Apr 11 '13 at 00:15
-
1should be noted that this doesn't always work in all situations and that the tester element should be appended to the body rather than after the input element – buggedcom Apr 14 '13 at 18:46
-
Is there any way to make this work for input fields that were added by jquery after the page was loaded? – bumbleshoot Sep 17 '13 at 03:49
-
We're using this plugin in production and made a few rather minor bugfixes. I'd like to publish this on GitHub - do you have a repository or may we publish this in ours while mentioning the original source, this SO question? Currently there's no license on the script - we would normally chose MIT. – Simon Steinberger Jul 05 '14 at 05:13
-
1Hi James, can I use this code as the basis for an official jQuery plugin on GitHub - released under MIT license? – Simon Steinberger Nov 26 '14 at 00:10
-
-
1And here it is: http://goodies.pixabay.com/jquery/auto-grow-input/demo.html Released under MIT on GitHub. Thanks for giving permission! The plugin is part of our tagEditor ( https://github.com/Pixabay/jQuery-tagEditor ), but I thought it should be an extra tool. – Simon Steinberger Dec 17 '14 at 17:45
-
Not sure if this is what most people would want, but I thought it worth adding a call to check at the end of the function after `$(this).bind('keyup keydown blur update', check);` `check();` – DanielM Apr 20 '15 at 15:42
-
This is still a great script. To add support for all text-y inputs (ie. HTML5 input types), you could simply remove the `.filter('input:text')`. – cabgfx Nov 03 '15 at 22:10
-
1I know this is old, but I thought I would point out that the jsfiddle does not seem to work. And it does not seem to have the code in the answer in it. – Vaccano Feb 06 '18 at 01:44
I have a jQuery plugin on GitHub: https://github.com/MartinF/jQuery.Autosize.Input
It uses the same approach as James answer but have some of the changes mentioned in the comments.
You can see an live example here: http://jsfiddle.net/mJMpw/6/
Example:
<input type="text" value="" placeholder="Autosize" data-autosize-input='{ "space": 40 }' />
input[type="data-autosize-input"] {
width: 90px;
min-width: 90px;
max-width: 300px;
transition: width 0.25s;
}
You just use css to set min/max-width and use a transition on the width if you want a nice effect.
You can specify the space / distance to the end as the value in json notation for the data-autosize-input attribute on the input element.
Of course you can also just initialize it using jQuery
$("selector").autosizeInput();

- 5,929
- 5
- 40
- 29
Good plugin, thank you! I changed two things that seemed to work better in my project though.
- I changed the TESTER tag to a DIV, to avoid getting 'Unexpected call to method or property access.' in IE8 (even though your demo does work in IE8. Was there a particular reason for for using a custom HTML tag?
- After the bind statement near the end of the code, I added a call to check(), in order to resize the textbox immediately after loading the page, in case the textbox already has content in it on startup.
Hope this helps.

- 987
- 6
- 2
-
2
-
When I run check function after loading page nothing happens, but onblur, onkeyup etc it works fine. Have anyone idea why? – kuboslav Mar 13 '12 at 10:13
just wanted to share a small improvement to James's great plugin. Add this code to the CSS declaration for the tester element to account for text-indent:
textIndent: 0
Without it, in some situations the tester element may inadvertently inherit the text-indent from elsewhere, thus throwing off the size of the input.
Like JP, I also wanted to resize the input to the correct size from the beginning, which I did just slightly differently, by chaining "trigger('keyup')" to the autoGrowInput method call, e.g.:
$('#contact_form').autoGrowInput({comfortZone: 7, minWidth: 10, maxWidth: 200}).trigger('keyup');
As a side note, I signed up to this site purely to comment on James's solution and I'm a bit annoyed to find that I can't because I don't have enough reputation points to start with. Sorry if I've missed something, but that seems to mean that I have to post this is a comment on the main question rather than more appropriately on James's solution.

- 548
- 4
- 11
I have create a plugin for the input of type text, that recreates this behaviour. It has some other unique features. You can see an example and view the documentation of the plugin. @james answer has some problems with pasting large text into the input. To fix it, I have made some modifications to his code. Here is a demo, for this example.
(function($){
$.fn.autoGrowInput = function(o) {
o = $.extend({
maxWidth: 200,
minWidth: 1,
comfortZone: 1,
width: 1
}, o);
this.filter('input:text').each(function(){
var minWidth = o.minWidth || $(this).width(),
maxWidth = o.maxWidth,
val = '',
input = $(this),
testSubject = $('<tester/>').css({
position: 'absolute',
top: -9999,
left: -9999,
width: 'auto',
fontSize: input.css('fontSize'),
fontFamily: input.css('fontFamily'),
fontWeight: input.css('fontWeight'),
letterSpacing: input.css('letterSpacing'),
whiteSpace: 'nowrap'
}),
check = function() {
if (val === (val = input.val())) {return;}
// Enter new content into testSubject
var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(/</g, '<').replace(/>/g, '>');
testSubject.html(escaped);
// Calculate new width + whether to change
var testerWidth = testSubject.width(),
newWidth = testerWidth + o.comfortZone,
currentWidth = input.width();
if (testerWidth < minWidth) {
newWidth = minWidth;
} else if (testerWidth > maxWidth) {
newWidth = maxWidth;
}
input.width(newWidth + o.comfortZone);
};
testSubject.insertAfter(input);
$(this).bind('input', check);
});
return this;
};
})(jQuery);

- 1,297
- 22
- 29
-
The "input" event is awesome! Just a heads up: the event doesn't work properly in IE: Deleting characters does not trigger this event in IE9 and older version don't have it at all. – Simon Steinberger Jul 05 '14 at 05:59
I'd also replaced
$(this).bind('keyup keydown blur update', check)
to
$(this).bind('keyup blur update', check).bind('keydown', function() {
setTimeout(check);
});
in order to get the field resized right after it was re-rendered by browser. It would rid the field from some chattering.

- 602
- 1
- 7
- 17
Awsome plugin James ! Thanks. I did add the check suggestion in the end by JP though very effective .
Also I added a some changes on my part. I wanted to set the size for the input to the maximum size if the changed width exceeded the maxWidth so I added :
else if (widthexceeds){
input.width(o.maxWidth);
}
below the if check for isValidWidthChange where widthexceeds = newWidth > o.maxWidth

- 867
- 8
- 16
Funny enough in IE overflow: visible is taken very seriously. You can achieve this effect by applying overflow: visible on your input elements. Not sure if any similar CSS tricks exist for modern browsers.

- 8,507
- 1
- 21
- 18
I created a plugin called input-autogrow
to solve this problem for my own projects. This plugin was originally based off of James answer but has been improved in many ways.
https://github.com/westonganger/input-autogrow
input-autogrow
is a plugin for autogrowing inputs. This plugin is different from others because most usually target textarea tags, this library is instead targeted at input tags. Requires a DOM library such as jQuery, Zepto, or any that supports $.fn plugins.
Here are some usage examples.
/* Makes elements readonly if they already have the readonly attribute */
$('input.autogrow').inputAutogrow();
/* Manually trigger update */
$('input.autogrow').trigger('autogrow');
/* or */
$('input.autogrow').trigger('change');
/* Custom Options */
$('input.autogrow').inputAutogrow({maxWidth: 500, minWidth: 25, trailingSpace: 10});
/* Remove autogrow from input */
$('input.autogrow').inputAutogrow('destroy');

- 6,324
- 4
- 41
- 39
If you want the textbox to grow when the string within it extends past its width, maybe something like this would work for you... It detects the size attribute of the textbox. If the length of the string goes over that attribute, it extends the textbox to the length of the string on keyup.
In the below script, "#test" is a textbox ID.
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#test").keyup(function(){
if($("#test").attr("size") < $("#test").val().length){
size = $("#test").val().length;
$("#test").attr("size",size);
}
})
});
</script>

- 7,125
- 4
- 33
- 42
-
1This relies on the text being a fixed-width typeface; so it won't work with fonts like Arial, verdana etc. – James May 31 '09 at 08:53