|
| 1 | +--- |
| 2 | +title: Dynamically Adjusting Height of RadTextBox Based on User Input |
| 3 | +description: Learn how to dynamically adjust the height of the RadTextBox control in Telerik UI for ASP.NET AJAX based on the number of lines entered by the user. |
| 4 | +type: how-to |
| 5 | +page_title: Adjusting RadTextBox Height Dynamically Using Client-Side Events |
| 6 | +meta_title: Adjusting RadTextBox Height Dynamically Using Client-Side Events |
| 7 | +slug: textbox-dynamically-adjust-height |
| 8 | +tags: telerik, radtextbox, onvaluechanged, client-events, resize, asp.net-ajax, multiline |
| 9 | +res_type: kb |
| 10 | +ticketid: 1692597 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | +<tbody> |
| 17 | +<tr> |
| 18 | +<td> Product </td> |
| 19 | +<td> Telerik® UI for ASP.NET AJAX RadTextBox </td> |
| 20 | +</tr> |
| 21 | +<tr> |
| 22 | +<td> Version </td> |
| 23 | +<td> All</td> |
| 24 | +</tr> |
| 25 | +</tbody> |
| 26 | +</table> |
| 27 | + |
| 28 | +## Description |
| 29 | + |
| 30 | +I want to dynamically adjust the height of a [RadTextBox](https://www.telerik.com/products/aspnet-ajax/documentation/controls/textbox/client-side-programming/events/onvaluechanged#onvaluechanged) control based on the number of lines entered by the user. My goal is to avoid vertical scrollbars while maintaining a fixed width of 400px. |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +To dynamically adjust the height of the RadTextBox control, use the `OnValueChanged` client-side event. Attach JavaScript logic to resize the text area as users type. The following example demonstrates how to achieve this: |
| 35 | + |
| 36 | +### Example |
| 37 | + |
| 38 | +Markup: |
| 39 | + |
| 40 | +````ASP.NET |
| 41 | +<telerik:RadTextBox ID="txtComment" runat="server" MaxLength="1024" Rows="5" TextMode="MultiLine" Width="400px"> |
| 42 | + <ClientEvents OnValueChanged="onValueChanged" /> |
| 43 | +</telerik:RadTextBox> |
| 44 | +```` |
| 45 | + |
| 46 | +````JavaScript |
| 47 | +function onValueChanged(sender, args) { |
| 48 | + let textArea = sender.get_element(); |
| 49 | + |
| 50 | + function resize() { |
| 51 | + textArea.style.height = 'auto'; // Reset height |
| 52 | + textArea.style.height = textArea.scrollHeight + 'px'; // Adjust to content height |
| 53 | + } |
| 54 | + |
| 55 | + textArea.addEventListener('input', resize); |
| 56 | +} |
| 57 | +```` |
| 58 | + |
| 59 | +Steps: |
| 60 | + |
| 61 | +1. Add the `OnValueChanged` client event to the RadTextBox. |
| 62 | +2. Implement the `resize()` function in JavaScript. It resets the height of the text area and adjusts it to match its scroll height. |
| 63 | +3. Use the `input` event listener to trigger the `resize()` function whenever the user types. |
| 64 | + |
| 65 | +This ensures the height dynamically updates based on the content entered by the user, avoiding vertical scrollbars while keeping the width fixed. |
| 66 | + |
| 67 | +## See Also |
| 68 | + |
| 69 | +- [RadTextBox Client-Side Events Documentation](https://www.telerik.com/products/aspnet-ajax/documentation/controls/textbox/client-side-programming/events/onvaluechanged#onvaluechanged) |
| 70 | +- [RadTextBox Overview](https://www.telerik.com/products/aspnet-ajax/documentation/controls/textbox/overview) |
0 commit comments