Skip to main content

A demonstration of an accessible method to hide form help utilising unobtrusive JavaScript.


Accessible JavaScript form help

Last revised: 15th August 2006

A rough example showing and hiding adjacent span elements intended as extended form help, using accessible unobtrusive JavaScript.

This work is a furtherance to a development by Chris Heilmann at

The demonstration shows & hides a piece of help text in the simple form below. The label text "Total amount to insure" is inside a span with a class="helpLink". The span is directly followed by another span which contains the help text to be hidden.

It should be noted for accessibility reasons both spans and the £ symbol are inside a label which is explicitly associated to the input.

Rough example

Fields marked * are required

The JavaScript is held in a separate file called showhide.js [1.59KB]. A minimal example is available for download: [1.56KB]

The (X)HTML code

Note: No link in the form; No JavaScript; All text inside the label.

<label for="sumInsured">
  Total amount to insure:
  <span class="helpLink">[help]</span>
  <span>You should state the new replacement value including VAT of the same or comparable replacements.</span>
  <strong class="required" title="Required">*</strong>
  <span>&pound; </span>
<input name="sumInsured" id="sumInsured" value="5000" size="10" maxlength="10" type="text" />


It is recommended for the help link to have a different look to the standard links on the page. the example uses greens and the cursor is set to "help".

strong.required {color:red; cursor:help}
a.helpLink:link {color:#040; cursor:help}
a.helpLink:hover {color:#090}
a.helpLink:visited {color:#020}

The unobtrusive JavaScript

The workhorse of the script is the function hideAll(). When the page has finished loading it looks through the content for all occurrences of a span element.

It then checks to see if the span has a class="helpLink". If so it sets the next adjacent span display property to disply:none. Then creates a link, populates it with the original text, and adds a few attributes.

An onclick action is then added which flips the display state between "none" and "inline", it also flips the link title accordingly. The link then finally replaces the original span.

function hideAll(){
  var obj,nextspan,anchor,content

  // get all spans

  // run through them
  for (var i=0;i<obj.length;i++){

    // if it has a class of helpLink

      // get the adjacent span
      while(nextspan.nodeType!=1) nextspan=nextspan.nextSibling

      // hide it'none'

      //create a link

      // copy original helpLink text and add attributes
      anchor.title='Click to show help'
      anchor.onclick= function(){
                        return false

      // replace span with created link

function changeTitle(obj) changes the span links title attribute accordingly.

function showHide(obj) changes the visibility of an object, in this case the next adjacent span.

function changeTitle(obj){
    obj.title = obj.title=='Click to show help' ? 'Click to hide help' : 'Click to show help'

function showHide(obj){
  if(obj) ='none' ? 'inline' : 'none'

window.addEventListener("load", hideAll, false);


The function hideAll() was a furtherance to a masterfully simple piece by Chris Heilmann at

Discussions on this technique are available on the Accessify forum.