jQuery - resizing form input based on value's width?

后端 未结 10 1369
挽巷
挽巷 2020-12-06 17:28

Is it possible to get input\'s value width and resize the input dynamically so the value will fit?

Here\'s an example:

http://jsfiddle.net/bzBdX/2/

10条回答
  •  渐次进展
    2020-12-06 17:43

    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);
    
    })​;
    

    http://jsfiddle.net/bzBdX/7/

提交回复
热议问题