Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

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!

Label Within The Form Field Handling

Today I'll show you how to implement something that appears easy at first glance but can be a bit tricky. It is the "label within the form field" functionality seen in forms that must take up very little space on a web page.

For example, instead of a "Your email address:" label to the left of the form field where the email address should be typed, the form field itself contains the label.

When the form field is clicked or tabbed into, the words in the form field disappear and the user can proceed to type the required information.

The tricky part is to make the JavaScript behave in a manner the user expects.

The straight-forward method of clearing the form field whenever it's clicked or tabbed into works okay most of the time. But if the user mis-types and then clicks back into the field to correct the error, the entire entry is cleared out again. That's unexpected, and not very professional.

The JavaScript must clear out the form field when it's clicked on or tabbed into only when the form field contains exactly what it contained when the web page was first loaded, and not if the form field was changed in any way.

If the user experiences an error and has to click back to the form, the label text should not overwrite information already in the form field. That, too, would be unexpected and unprofessional.

This is the way to do it, first the form and then the JavaScript:

<form 
   name="MyForm" 
   method="POST" 
   action="/cgi-bin/script.cgi">
<input 
   type="text" 
   name="email" 
   onfocus="ClearIfAppropriate();">
<input 
   type="submit">
</form>

<script type="text/javascript" language="JavaScript"><!--
var LabelText = "Type your email here";

if(document.MyForm.Email.value.length == 0) {
	document.MyForm.Email.value = LabelText;
	}

function ClearIfAppropriate() {
if(document.MyForm.Email.value == LabelText) {
	document.MyForm.Email.value = "";
	}
}
//--></script>

The text the JavaScript will insert into the form field can be changed. See the second line of the JavaScript, the line beginning with "var LabelText ="

The "MyForm" between the dots must be changed at every occurrence in the JavaScript if the name you give your form is something other than "MyForm".

Similarly, the "Email" between the dots must be changed at every occurrence in the JavaScript if the name you give the form field is something other than "Email".

Putting the JavaScript below the form ensures the browser has seen the form before the JavaScript tries to insert the label text into the form field.

Before the JavaScript inserts the text, it checks to verify the field is empty. This is in case the user experiences an error message and has to click back to the form. If the form already contains information, the JavaScript does not overwrite it.

When the user clicks the form field or tabs into it, the JavaScript checks to see whether or not it should be cleared.

Because the label text is specified within the JavaScript itself, it knows what to look for when comparing what's in the form field now with what it contained when the page first loaded. Only if the two are identical will the form field be cleared when the user clicks or tabs into it.

Now you know how to implement "label within the form field" functionality that works like user's expect it to.

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