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

后端 未结 10 1357
挽巷
挽巷 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:33

    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 on personal preference / text-size

    Example : http://jsfiddle.net/bzBdX/6/

提交回复
热议问题