There are typically only two types of forms that tend to get squeezed into tight quarters in page layouts: login, and search. For this reason, web designers are often tempted to save space by hiding the labels on these forms and assuming that a user will intuitively know what to type into each textbox. With a simple search form, this is logical because you have a textbox next to a button that says “Search”, “Go”, or perhaps “I’m feeling Lucky”. Login forms are a little trickier. For most of us, seeing two text boxes and a “login” button is all we need to play McGyver, but the average user often needs those “Username:” and “Password:” labels to know what to enter.
A common solution to this problem is to use javascript to show and hide “hints”. This is one of those tasks that has a dozen different (perfectly acceptable) methods, and everyone tends to stand behind their choice as boldly as they do their favorite presidential candidate. I’ve tried a few of these methods, but because I tend to do it a bit differently, I thought I’d share.
Most of the solutions out there either insert a value into the textbox or they move around a label. Inserting a value into a textbox is the most obvious/common solution and has its pros and cons, but the deal breaker is the fact that a value inserted into a password box will still display as dots/asterisks. As far as I know, you can’t dynamically change the type of an input from password to text, so that solution is out. Moving around a label tag is something I first read about on the ALA article: Making Compact Forms More Accessible. It’s an ingenious idea really, but:
With those things in mind, I decided to try using a background image for on the textbox instead of the label. Also, I tend to incorporate jQuery into most projects lately, so it made sense for me to write it in that - although it could just as easily be rewritten as straight Javascript if necessary.
Ove at 3:05 PM on Thursday, Jan 31, 2008
Nice one.
The sorry thing is that there is support to do this dynamically (changing the TYPE attribute value), but for some reason IE just won't comply. I think it's getting confused and mixes up TYPE in Javascript and the DOM.
Ugly inline example, works in FF, not in IE.
<input type="text" value="Password Here" onclick="this.type='password';this.value='';">
Ken Seals at 3:26 PM on Thursday, Jan 31, 2008
Very nice. I get to play with forms pretty much every day.. I'll definitely be making use of this. Thanks, man!
One bit that caught my eye is using display: none to hide the labels. It might be better to throw it off screen with absolute positioning and a negative margin or something, as some screen readers will actually take into account display: none and hide the label from visually impaired users.
Jason Beaird at 5:38 PM on Thursday, Jan 31, 2008
@Ove: Yea, defintiely tried that. Don't you just love IE.
@Ken: I thought about that but in an attempt to keep the css pretty simple I just display noned it. I'll update the example tonight.
If you have a Gravatar, the image above will be replaced by it when you post your comment.
Darragh G (Seachmall) at 1:43 PM on Thursday, Jan 31, 2008
I normally just display a styled alt under the box via JS/CSS but that way seems alot smoother, very nice.