javascript - How can I trim an input element's width to the size of the initial text? -


Should I be sufficiently large enough to determine the initial width of an input element Initial Text for? Example of what I want to get:

I tried to set the property width but it does not give me what I want:

   

I get either more white space or the text is truncated.

Is this possible?

I'm targeting HTML5 browsers only (IE9 +, Chrome, FF), so the css3 solution will be OK. Javascript solutions are also welcomed.

Thanks

I think the easiest solution is to use the width set To jQuery:

  $ Each ($ ('input'), function () {$ (this) .css ('width', (($ (this) .val (). Length) * 6 + 'px'));};  

Multiply by 6 seemed the best with default font. If you have used custom fonts for input, you may need to adjust it

Demo:


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -