Use an onkeydown handler in your text field, measure the text*, and increase the text box size accordingly.
Attach a :focus css class to your text box with a larger width. Then your box will be larger when focused. That's not exactly what you're asking for, but similar.
* It's not straightforward to measure text in javascript. Check out this question for some ideas.
var jqThis = $('#adjinput'), //object of the input field in jQuery
fontSize = parseInt( jqThis.css('font-size') ) / 2, //its font-size
//its min Width (the box won't become smaller than this
minWidth= parseInt( jqThis.css('min-width') ),
//its maxWidth (the box won't become bigger than this)
maxWidth= parseInt( jqThis.css('max-width') );
jqThis.bind('keydown', function(e){ //on key down
var newVal = (this.value.length * fontSize); //compute the new width
if( newVal > minWidth && newVal <= maxWidth ) //check to see if it is within Min and Max
this.style.width = newVal + 'px'; //update the value.
});
EDIT: Another solution is to havethe user type what he wants and on blur (focus out), grab the string (in the same font size) place it in a div - count the div's width - and then with a nice animate with a cool easing effect update the input fields width. The only drawback is that the input field will remain "small" while the user types. Or you can add a timeout : ) you can check such a kind of solution on the fiddle above too!
Making an HTML element contenteditable lets users paste copied HTML elements inside of this element. This may not be ideal for your use case, so keep that in mind when choosing to use it.
How about programmatically modifying the size attribute on the input?
Semantically (imo), this solution is better than the accepted solution because it still uses input fields for user input but it does introduce a little bit of jQuery. Soundcloud does something similar to this for their tagging.
I know this is a seriously old post -
but my answer might be useful to others anyway, so here goes.
I found that if my CSS style definition for the contenteditable div has a min-height of 200 instead of a height of 200 , then the div scales automatically.
Which approach you use, of course, depends on what your end goal is. If you want to submit the results with a form then using native form elements means you don't have to use scripting to submit. Also, if scripting is turned off then the fallback still works without the fancy grow-shrink effects. If you want to get the plain text out of a contenteditable element you can always also use scripting like node.textContent to strip out the html that the browsers insert in the user input.
This version uses native form elements with slight refinements on some of the previous posts.
It allows the content to shrink as well.
Use this in combination with CSS for better control.
Here's a method that worked for me. When you type into the field, it puts that text into the hidden span, then gets its new width and applies it to the input field. It grows and shrinks with your input, with a safeguard against the input virtually disappearing when you erase all input. Tested in Chrome. (EDIT: works in Safari, Firefox and Edge at the time of this edit)
function travel_keyup(e)
{
if (e.target.value.length == 0) return;
var oSpan=document.querySelector('#menu-enter-travel span');
oSpan.textContent=e.target.value;
match_span(e.target, oSpan);
}
function travel_keydown(e)
{
if (e.key.length == 1)
{
if (e.target.maxLength == e.target.value.length) return;
var oSpan=document.querySelector('#menu-enter-travel span');
oSpan.textContent=e.target.value + '' + e.key;
match_span(e.target, oSpan);
}
}
function match_span(oInput, oSpan)
{
oInput.style.width=oSpan.getBoundingClientRect().width + 'px';
}
window.addEventListener('load', function()
{
var oInput=document.querySelector('#menu-enter-travel input');
oInput.addEventListener('keyup', travel_keyup);
oInput.addEventListener('keydown', travel_keydown);
match_span(oInput, document.querySelector('#menu-enter-travel span'));
});
For those strictly looking for a solution that works for input or textarea, this is the simplest solution I've came across. Only a few lines of CSS and one line of JS.
The JavaScript sets a data-* attribute on the element equal to the
value of the input. The input is set within a CSS grid, where that
grid is a pseudo-element that uses that data-* attribute as its
content. That content is what stretches the grid to the appropriate
size based on the input value.
All you need to do is, get the element of the input field you want to grow as you type and in CSS, set the width of the input to auto and set a min-width to say 50px.
.growing-input {
display: inline-grid;
}
.growing-input .dummy,
.growing-input input {
grid-area: 1 / 1;
/* Following properties just need to be consistent,
to ensure the .dummy and the input take up the same space */
font: inherit;
padding: 0 0.25em;
margin: 0;
border: 1px solid grey;
border-radius: 2px;
}
.growing-input .dummy {
visibility: hidden;
white-space: pre-wrap;
}
Here's an
<span class="growing-input">
<input type="text" value="auto-resizing input" size="1" />
<span class="dummy"></span>
</span>; have fun!
The idea is to create a dummy element containing the same content as the input, then set the input width to match that of the dummy element. Above we use JavaScript to synchronize the text, and a display: inline-grid trick to set the input width to match.
(This approach is taken from this article; I've condensed it to the bare essentials.)