The method of changing the length of an input text box according to its content



Copy code

The code is as follows:

<input type=”text” onkeydown=”this.onkeyup();” onkeyup=”this.size=(this.value.length>4?this.value.length:4);” size=”4″>

Where size = “4” is to define the initial size. If it is not defined here, then this. Size = (this. Value. Length > 4? This. Value. Length: 4); the 4 in it is changed to 20, and the default size of < input type = “text” > is 20

If you define the width attribute in style, and make its width change according to the content, the two requirements are not contradictory. Therefore, you cannot define the width attribute in style here