This question already has an answer here:
How do I auto-resize the input type="text" field with jQuery? I want it to be like 100px wide at the start, then make it auto-widening as user inputs text... is that possible?
This question already has an answer here:
How do I auto-resize the input type="text" field with jQuery? I want it to be like 100px wide at the start, then make it auto-widening as user inputs text... is that possible?
I used seize's answer, but made these changes:
Between setting
isValidWidthChange
and// Animate width
:This way the input will grow as big as you've allowed it when its contents are too big to fit within the max width.
After
$(this).bind('keyup keydown blur update', check);
:See this jQuery plugin:
https://github.com/padolsey/jQuery.fn.autoResizeI just tested it out with textareas and it works! Supports auto-growing of textareas, input[type=text] and input[type=password].
UPD. Looks like the original author removed the repo from github. The plugin hasn't been updated in a long while and turned out to be quite buggy. I can only suggest you to find a better solution. I made a pull request to this plugin sometime ago so I have a copy of it in my github account, use it only in case you want to improve it, it's not bulletproof!
Also, I found that ExtJS framework has autosize implementation for text fields, see the grow config property. While it's not that easy to carve this little piece of logic out of the framework, it can give you some good ideas on the approach.
Here's a plugin that'll do what you're after:
The plugin:
EDIT: Found on: Is there a jQuery autogrow plugin for text fields?
Try this code:
(Edited: using the
.text()
method instead of.html()
to get all formatting right.)Hello I dont know if you are still looking but i came across this when i was looking for a script to do the same thing. So hope this helps anyone who is trying to do this, or something similar.
The logic to this code is to put the content onto the page in a span and then gets the width of this content and removes it. The problem i did have was with it was that i had to get it to run on both keydown and keyup for it to work successfully.
Hope this helps, Might not as i have only been doing jquery for a short amount of time.
Thanks
George
I have a jQuery plugin on GitHub: https://github.com/MartinF/jQuery.Autosize.Input
It uses the same approach as seize's answer but have some of the changes mentioned in the comments.
You can see an live example here: http://jsfiddle.net/mJMpw/6/
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