[ACCEPTED]-jQuery - resizing form input based on value's width?-resize
Here is the jQuery code :
$('input').each(function(){
var value = $(this).val();
var size = value.length;
// playing with the size attribute
//$(this).attr('size',size);
// playing css width
size = size*2; // average width of a char
$(this).css('width',size*3);
});
0
I have a jQuery plugin on GitHub: https://github.com/MartinF/jQuery.Autosize.Input
It mirrors 11 the value of the input so it can calculate 10 the actual length instead of guessing or 9 other approaches mentioned.
You can see an 8 live example here: http://jsfiddle.net/jw9oqz0e/
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 7 css to set min/max-width and use a transition 6 on the width if you want a nice effect.
You 5 can specify the space / distance to the 4 end as the value in json notation for the 3 data-autosize-input attribute on the input 2 element.
Of course you can also just initialize 1 it using jQuery
$("selector").autosizeInput();
Current answers were waay to complicated. Heres 2 a quick one
$('#myinput').width($('#myinput').prop('scrollWidth'))
Adding the above to input keydown/up/press 1 events is trivial. Tested in chrome
Something simple :
$('#resizeme').keydown(function(){ // listen for keypresses
var contents = $(this).val(); // get value
var charlength = contents.length; // get number of chars
newwidth = charlength*9; // rough guesstimate of width of char
$(this).css({width:newwidth}); // apply new width
});
You could change the multiplier 2 on personal preference / text-size
Example 1 : http://jsfiddle.net/bzBdX/6/
$(function(){
$("input").keyup(function(){
$(this).stop().animate({
width: $(this).val().length*7
},100)
})
})
0
So i made an example where it tries to calculate 3 the width by inserting letters in a span 2 and calculating there width
(function() {
var mDiv = $("<span />").text("M").appendTo("body"),
mSize = mDiv.width();
mDiv.detach();
var letterSize = function(s) {
var sDiv = $("<span />").text("M" + s + "M").appendTo("body"),
sSize = sDiv.width();
sDiv.detach();
return (sSize - (mSize * 2)) * 0.89;
};
$("input[data-resise-me]").each(function() {
var minSize = $(this).width();
var resizeInput = function() {
var calcSize = $.map($(this).val(), letterSize);
var inputSize = 0;
$.each(calcSize, function(i, v) { inputSize += v; });
$(this).width( inputSize < minSize ? minSize : inputSize );
};
$(this).on({ keydown: resizeInput, keyup: resizeInput });
});
} ());
Theres probably 1 a much better way of doing this.
As mentioned by @ManseUK. This line worked 1 for me. JQuery version 1.8
$('#resizeme').css({width:newwidth});
All those solutions based on number of characters 6 are quite weak, because each character could 5 be of a different width if it is not a monotype 4 font. I'm solving this problem with creating 3 a div containing value of text input with 2 the same font properties and getting its 1 width as a required one.
Something like this:
function resizeInput() {
$('body').append('<div class="new_width">'+$(this).val()+'</div>');
$(this).css('width', $('.new_width').width());
$('.new_width').remove()
}
$('input')
// event handler
.keyup(resizeInput)
// resize on page load
.each(resizeInput);
this example as been tested on Chrome 25 1 and Firefox 19. (http://jsfiddle.net/9ezyz/)
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);
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.