Michael Swarts
Michael Swarts

Reputation: 3921

HTML input field width (related to JavaScript)

I want to make an input field that has a min-width of 25px and a width of 10px per entered character. Therefore, each character entered after the second will increase the width of the input field. Lastly, the input field should become smaller when characters are deleted.

I just need an idea of how to solve the variable width based on input characters. Suggestions?

Thanks!

Upvotes: 1

Views: 812

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

This is trivial.

<input type="text" style="width: 25px;"
               onKeyUp="this.style.width = Math.max(25,this.value.length*10)+'px';" />

Upvotes: 3

Related Questions