Gloss & Stripes

About Programming, Mac, Design and Everything that's somehow Arty or Open Source.

Clickable Form Labels for Safari and IE [en]

Usability is very important - especially when the user has to take the challenge of filling in a web-form. Form labels give textual information as well as an interactive association. Unfortunately, Safari and Internet Explorer don't spend much attention to that linkage. This article presents a script that adds click functionality to all form labels on a website for Safari and Internet Explorer.

In the W3C-Specs for the label element it says:

When a LABEL element receives focus, it passes the focus on to its associated control. (from: W3C Specifications for the label)

...which means the form element will be selected if the label is clicked.

This association is handled by the label's for attribute and has to be identical to the id of the input field. Unfortunately, Safari and Internet Explorer don't spend much attention to this association - so users of these browsers have a hard time when it comes to aiming at 10x10 pixel sized checkboxes to toggle them.

The following script adds click functionality to all form labels on a website for Safari and Internet Explorer. It works for radio buttons, checkboxes, text fields, Textareas and select elements.

The script works like this:

  1. Check if browser is Safari or IE — the following is only processed for these two browsers.
  2. Grab all label elements
  3. Add an onclick event for every label:
    1. Get the element with the id of the labels for attribute.
    2. Set focus on textfields and textareas, toggle checkboxes and radio buttons

JavaScript for IE and Safari

JavaScript:
  1. function fixFormLabels(){
  2. var labels;
  3.    
  4. // enable for IE and Safari
  5. if( document.all || navigator.userAgent.indexOf("Safari")> 0){
  6.     labels = document.getElementsByTagName("label");
  7.     for(i=0; i<labels.length; i++){
  8.         labels[i].onclick = function(){
  9.             var target = document.getElementById(this.getAttribute('for'));
  10.             // Checkboxes or radio button labels
  11.             if(target.type == 'checkbox' || target.type == 'radio'){
  12.                 target.checked = target.checked == false ? true : false;
  13.             }
  14.             else{ // Textareas and input fields, Select elements
  15.                 target.focus();
  16.             }
  17.         };
  18.     }
  19. }
  20. }
  21.  
  22. // execute the script when the page has loaded
  23. window.onload = fixFormLabels;

A Version based on prototype.js

JavaScript:
  1. function fixFormLabels(){   
  2. var labels;
  3.    
  4. // enable for IE and Safari
  5. if( document.all || navigator.userAgent.indexOf("Safari")> 0){
  6.     labels = document.getElementsByTagName("label");
  7.     $A(labels).each ( function(label){
  8.         Event.observe(label, "click", function(){
  9.             var target = $(this.getAttribute('for'));
  10.             // Checkboxes or radio button labels
  11.             if(target.type == 'checkbox' || target.type == 'radio'){
  12.                 target.checked = target.checked == false ? true : false;
  13.             }
  14.             else{ // Textareas and input fields, Select elements
  15.                 target.focus();
  16.             }
  17.         });
  18.     });
  19. }
  20. }
  21.  
  22. // execute the script when the page has loaded
  23. Event.observe(window,"load", fixFormLabels);

Indicating Interactivity with CSS

Now make sure that you include a rule that turns the mouse pointer to the hand (usually) which indicates a clickable link.

CSS:
  1. label{ cursor: pointer; }

Alternatives

There's also a solution that uses JavaScript behaviour in CSS. That method implies that you simply need to add class="clickable" to the label element.
The downside of this is the use of the attribute selector label[class="clickable"]{ behaviour: none;} to reset the behaviour for Firefox. Unfortunately, Safari also understands this rule - so form labels aren't clickable anymore.

Therefore my solution is suited better for 'cross-browser' use, as it also makes Safari users happier than they already are.

Example

As written above the association between the label and the input field is done by the for attribute of the label. It's content has to be identical to the id of the input field.
A correct implementation would look like this:

HTML:
  1. <label for="username">Your Nickname</label>
  2. <input type="text" id="username" />

As the JavaScript above is already implemented in this blog, the following example has the fix applied. Just test it by clicking on the label.

Or scroll down and try it on the commentform. And while you're there you are welcome to leave me a message wheter you agree with my solution or not.

RT @dertimbo Clickable Form Labels for Safari and IE

Posted in Developing, Tags:, , ,

Possibly Related Entries

One Response to “Clickable Form Labels for Safari and IE”

  1. Cotter said

    Thanks for the script. I understand this is great for users of Safari versions prior to version 3, but the script also targets Internet Explorer. Why is this so? I’ve tested forms in Internet Explorer 6, 7 and 8 on Windows and even in IE5 on Mac and all of them seem to focus on associated fields when their labels are clicked. Is this perhaps targeted at IE 4, 3, 2 or 1? I’d be interested in knowing what the reason for including IE in this was.

Post a comment or review the article

+ -

Who is this?

freshlabs journal is the bi-lingual weblog and digital playground of Tim Isenheim, designer and webdeveloper from Hamburg, Germany. More →

  • Download Summersun, a sunny theme for WordPress
  • Download WP SIMILE Timeline, a lifestream plugin for WordPress

Topics

Archives

Tag Cosmos

3D ajax Apple blogging browser comedy comments contest copenhagen css Design dom firefox flash fuckedup gallery google html interface javascript music mythbusters offline opensource osx photo photoshop php plugin powerbook programming projects psp skype Software spam studyabroad switch video w3c wallpaper webdesign webstandards westciv Wordpress

Full Tag Cloud