Alison Posted July 24, 2018 Report Share Posted July 24, 2018 Hello, Is there a way to make a text box dynamically change for the inputted text? Quote Link to comment Share on other sites More sharing options...
1 vinebath Posted July 24, 2018 Report Share Posted July 24, 2018 Hello Alison, If you have this element in the HTML: <input type="text" id="txtbox" /> you can apply the following JS code instead: var txtBox = document.getElementById('txtbox'); document.addEventListener("keydown", function(event) { if(txtBox.value.length>20){ if(event.keyCode!=8) //backspace check txtBox.style.width = (( txtBox.value.length+1) * 8) + 'px'; //extends the field by 1 character width when key is pressed else txtBox.style.width = (( txtBox.value.length-1) * 8) + 'px';} //shrinks the field by 1 character width when backspace is pressed }); This code will dynamically adjust the width of your field depending on the number of characters. Feel free to adjust this to your needs if something is not meeting your expectations. Hope this helps! Quote Link to comment Share on other sites More sharing options...
0 IMDora Posted July 24, 2018 Report Share Posted July 24, 2018 Hi Alison, What do you mean by to make a textbox to dynamically change for inputted text? - IMDora Quote Link to comment Share on other sites More sharing options...
0 Alison Posted July 25, 2018 Author Report Share Posted July 25, 2018 Perfect! Thank you, vinebath. That is exactly what I need Quote Link to comment Share on other sites More sharing options...
Question
Alison
Hello,
Is there a way to make a text box dynamically change for the inputted text?
Link to comment
Share on other sites
3 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.