How to resize a textbox on the fly.

Posted on 2004-10-25
Last Modified: 2008-02-26
I have an input type=text  readonly.  I want this textbox to be able to resize based on the length of the input that will populate it.  How can I do this??  I am using HTML and javascript.
Question by:mtiGirl
    LVL 14

    Accepted Solution

    In a web page there is no good way to really pin down the size of text ... but you could make your best stab at it by checking the length of the value of the textbox and hopefully knowing the font family and font size you can make a decent attempt to size the textbox's width.

    var txtBox = document.getElementById("myTextBox");
    var length = txtBox.value.length; = (length * 5) + "px";

    try adjusting the 5 to whatever seems to work best ...
    LVL 3

    Expert Comment

    You can use this script for what you're looking for.  They have several examples to show you what it does.  The difference for you would be that the text box would be disabled and the size would default to the length of the text.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now