How to Adjust the Width of Input Field Automatically using JavaScript? Last Updated : 12 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The <input> element in HTML is used to create interactive controls for forms, allowing users to input data. It is one of the most versatile elements, with various input types and attributes that make it powerful for building forms. One important attribute is the width attribute, which allows you to control the width of the input field.1. Using onkeypress Event in JavaScriptThe onkeypress event triggers when the user presses a key. By tracking the input length with this.value.length, we can dynamically adjust the width of the <input> element to match the length of the value entered by the user.Use the onkeypress event on the <input> element.Track the length of the input field with this.value.length.Adjust the width of the input dynamically based on the length of the entered text.<input type="text" onkeypress="myFunction()"> Here, we select the <input> element and add a method to it which occurs when a key is pressed. This method selects and updates the value of the width property dynamically. You can also write a JavaScript function to check the length of your string on input change, then adjust the width of the input based on the number of chars * character width. HTML <form method="post" action=""> <label for="username">Input text</label> <input type="text" id="textbox" name="textbox" placeholder="Welcome" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';" id="input" /> </form> JavaScript const input = document.querySelector('input'); input.style.width = ((input.getAttribute('placeholder').length + 1) * 8) + 'px'; input.addEventListener('focusout', function() { if (this.value.length > 0) { this.style.width = ((this.value.length + 1) * 8) + 'px'; } else { this.style.width = ((this.getAttribute('placeholder').length + 1) * 8) + 'px'; } }); Output2. Using jQuery keypress() EventThe jQuery keypress() event, in combination with String.fromCharCode(e.which), can be used to dynamically increase the width of the input element. This approach calculates the width of the input based on the number of characters entered and adjusts it accordingly.Use the jQuery keypress() event to detect keypresses.Use String.fromCharCode(e.which) to get the character pressed.Dynamically adjust the width of the input field based on the input length. JavaScript $('input[type="text"]').keypress(function(e) { if (e.which !== 0 && e.charCode !== 0) { // Only characters var c = String.fromCharCode(e.keyCode|e.charCode); $span = $(this).siblings('span').first(); // The hidden span takes $span.text($(this).val() + c) ; // The value of the input $inputSize = $span.width() ; // Apply width of the span to the input $(this).css("width", $inputSize) ; } }) ; Output Comment More infoAdvertise with us Next Article How to disable browser autofill on input fields using jQuery ? S SonaliPatel Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc Similar Reads How to change Input box borders after filling the box using JavaScript ? In this article, we will change the input border after filling in the text on the input text field. The onchange event attribute works when the value of the element changes and selects the new value from the list. Approach: Whenever the input value is changed with some value by the user, the onchang 2 min read How to Change Border Width of Div in JavaScript ? Div elements are essential in creating modern web applications, and manipulating their properties dynamically can enhance user experience. In this article, we'll see how to change the border width of a div element using JavaScript. The border-width property in CSS is used to specify the width of the 2 min read Calculate the width of the text in JavaScript Calculating the width of text in JavaScript refers to determining the pixel width that a specific string of text will occupy when rendered in a browser. This is commonly done using the Canvas API or by creating a hidden element with similar styling to measure the text.Here we have some common approa 3 min read How to disable browser autofill on input fields using jQuery ? In this article, we will see how to disable the browser auto fill property on the input field. For that, HTML page is created in which jQuery CDN is imported to use jQuery and its code is written. Approach: Create a basic HTML page having at least one input field in it with the "id" attribute.Import 2 min read Allow users to change font size of a webpage using JavaScript If you have seen some websites, especially some Indian government portals, you would have noticed three small buttons at the top rightmost corner of the page looking something like this: -A A A+. These buttons generally allow website users to change the font size of the entire page or a certain port 3 min read Set the focus to HTML form element using JavaScript To set focus to an HTML form element, there can be many methods that can be used to focus. In this article, we will discuss them all. These are the following approaches: Table of Content Using focus() methodUsing window.onload methodApproach 1: Using focus() methodIn this approach, we are going to u 2 min read Like