Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Automatically Enlarge Form Field When Needed

Here is a little something to have fun with.

A text field grows larger as the site visitor types characters into it. It shrinks as characters are deleted.

The form field has an ID. It also has an onkeyup attribute. Whenever a character is typed into the field, a JavaScript is called with the field's internal identity and ID value.

The JavaScript adjusts the size="..." attribute of the field to be one character larger than the number of characters already in the field. The size stays within a maximum and a minimum.

Here is an example. The field's minimum size is 5 and its maximum size is 30.

Type something:

Here is the code to reproduce the above example.

<form>
Type something: 
<input 
   id="myid" 
   style="font-family:monospace; font-size:14px;" 
   name="bignumber" 
   size="5" 
   onkeyup="ResizeField(this,'myid')">
</form>

<script type="text/javascript"><!--
function ResizeField(f,id) {
var minimum = 5;
var maximum = 30;
var size = 1 + f.value.length;
if(size > maximum) { size = maximum; }
else if(size < minimum) { size = minimum; }
document.getElementById(id).size = size;
}
//--></script>

Note the JavaScript call in the form field's onkeyup attribute. The function ResizeField() is called with two attributes: this and 'myid'

The this attribute is not within quotes. The word is a variable containing the field's internal identity.

The 'myid' attribute is quoted. It is the ID value assigned to the field.

ResizeField() uses those two attributes to determine the number of characters already in the field and to adjust the field's size="..." attribute.

In the JavaScript, you'll see

var minimum = 5;
var maximum = 30;

Adjust the numbers to represent the minimum size and the maximum size of the field on your web page.

Have a little fun :)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC