Results 1 to 2 of 2

Thread: auto resizing text field

  1. #1
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Question auto resizing text field

    I wold like a javascript code that can automatically resize a textfield.
    example: there is a textfield of size 6 if the content crosses limit the text
    field will regularly hide before content. But i want it to expand its size and adjust to show the complete text!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This seems to work out fairly well in limited testing:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function reSizeField(field, e){
    if (e && e.keyCode){
    // document.getElementById('kc').value=e.keyCode; //diagnostic use only
    if (e.keyCode>=36 && e.keyCode<=40 || e.keyCode==20 || e.keyCode==9) return;
    }
    var r=document.getElementById('resizer');
    r.firstChild.nodeValue = field.value;
    if (e && e.keyCode && e.keyCode!=46)
    field.value = '';
    field.style.width = Math.max(document.getElementById('reference').offsetWidth, r.offsetWidth)+'px';
    if (e && e.keyCode && e.keyCode!=46)
    field.value = r.firstChild.nodeValue;
    }
    </script>
    </head>
    <body>
    <input id="reference" type="text" size="6" style="font-size:90%;font-family:sans-serif;border:none;position:absolute;top:-1000px;left:-1000px;visibility:hidden;">
    <span id="resizer" style="font-size:90%;font-family:sans-serif;position:absolute;top:-1000px;left:-1000px;visibility:hidden;">&nbsp;</span>
    <input type="text" style="font-size:90%;font-family:sans-serif;" size="6" onkeyup="reSizeField(this, event);" onmousemove="reSizeField(this, event);" onchange="reSizeField(this, event);"><br>
    <!-- <input type="text" id="kc"> diagnostic use only -->
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •