I want to use jQuery to expand the width of a input textbox during onclick.
For example, if the input has a default width of 100px, when the user clicks in it to type, it will expand to like 150px in width.
Is it possible to do this?
Thanks.
Here's an updated example on jsfiddle that animates, and returns to the default size on blur.
For reference, see jQuery Animate docs.
I'd like to build on @wsanville's example by removing the extra step on saving the original size. Animation also takes string values like '+=50' or '-=50' for increasing/decreasing the width.
See in in action on jsfiddle
$('#box').focus(function()
{
$(this).animate({ width: '+=50' }, 'slow');
}).blur(function()
{
$(this).animate({ width: '-=50' }, 'slow');
});
I know this is a bit late to the party, but if anyone wants a pure CSS solution that uses the :focus attribute.
#textbox {
width:100px;
transition: 0.5s;
}
#textbox:focus {
width:150px;
transition: 0.5s;
}
Here's my jsFiddle:
https://jsfiddle.net/qo95uquv/
Tom
Here a working example based on @wsanville's example with the requested delay. (Note I also added an effect to return it on loss of focus, you can remove that easily)
Working Example
**edited the size of the textbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With