0

How can I set the with of '<input>' element equal to the content with? (The content is dynamic.)

example: http://jsfiddle.net/WG86g/

3 Answers3

1

See if this works for you

<input id="chk1" type="text" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';">​

The problem is that multiples of 8 are handled.

S.P.
  • 3,054
  • 1
  • 19
  • 17
0
$("input").css("width",$(this).prop("scrollWidth"));
0

you should use this. simple and easy

$('#textbox').autoResize();

http://james.padolsey.com/javascript/jquery-plugin-autoresize/

EDIT:

James Padolsey left to develop this plugin. So I would recommend you to use Jack Moore's plugin. If you want to use old js file, you can access js file from here.

http://james.padolsey.com/demos/plugins/jQuery/autoresize.jquery.js/view

https://github.com/jackmoore/autosize

http://www.jacklmoore.com/autosize

Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60