function input_update_size()
{
var value = $(this).val();
var size = 12;
// Looking for font-size into the input
if (this.currentStyle)
size = this.currentStyle['font-size'];
else if (window.getComputedStyle)
size = document.defaultView.getComputedStyle(this,null).getPropertyValue('font-size');
$(this).stop().animate({width:(parseInt(size)/2+1)*value.length},500);
//$(this).width((parseInt(size)/2+1)*value.length);
}
$('input')
.each(input_update_size)
.keyup(input_update_size);
I have a jQuery plugin on GitHub: https://github.com/MartinF/jQuery.Autosize.Input
It mirrors the value of the input so it can calculate the actual length instead of guessing or other approaches mentioned.
You can see an live example here: http://jsfiddle.net/jw9oqz0e/
Example:
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
As mentioned by @ManseUK. This line worked for me. JQuery version 1.8
Current answers were waay to complicated. Heres a quick one
Adding the above to input keydown/up/press events is trivial. Tested in chrome
this example as been tested on Chrome 25 and Firefox 19. (http://jsfiddle.net/9ezyz/)