Home > Article > Web Front-end > js method to implement text box width adaptive text width_javascript skills
The example in this article describes how to implement text box width adaptive text width in js. Share it with everyone for your reference. The details are as follows:
A JS code that will automatically increase the width according to the number of characters entered into the text box. When we enter characters in the text box, if the width of the text box is defined too small, then the characters we enter will be Hidden, this code realizes that the text box will automatically adapt to the amount of input text, and it will automatically lengthen.
The operation effect is as shown below:
The specific code is as follows:
<!Doctype HTML PUBLIC "-//W3c//DTD Html 1.0 Transitional//EN"> <html> <head> <title>文本框宽度自动适应文本宽度</title> </head> <script type="text/javascript"> function changeInputlength(cursor) { var getcount=document.getElementById("countFont"); var getText=document.getElementById("text"); getcount.innerHTML='<font>第'+(parseInt(getText.value.length)+1)+'个字符</font>'; cursor.size=getText.value.length+2; } </script> <body> 请输入字符:<input type="text" size="3" id="text" onkeydown="changeInputlength(this);"/> <span id="countFont"></span> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.