Field Help DOM/JavaScript

Field Help Unobtrusive DOM/JavaScript Code

Linking to the external DOM/JavaScript file

  1. <head>
  2.   <script type="text/javascript" src="scripts/fieldhelp.js"></script>
  3. </head>

top

The DOM/JavaScript Code

Use this Unobtrusive DOM/JavaScript to initiate Context-Sensitive Field Help.

Below is the external DOM/JavaScript code which opens and closes the Context-Sensitive help text below each field name when triggered by a user activating a field help icon.

  1. /* start addLoadEvent(func) function - allows scripts to load/register gracefully - design by Simon Willison */
  2. function addLoadEvent(func)
  3. {
  4.   var oldonload = window.onload;
  5.   if (typeof window.onload != 'function') {
  6.     window.onload = func;
  7.   } else {
  8.     window.onload = function() {
  9.       if (oldonload) {
  10.         oldonload();
  11.       }
  12.       func();
  13.     }
  14.   }
  15. }
  16. addLoadEvent(init);
  17. addLoadEvent(function() { 
  18. /* add more code here to run on page load */
  19. addLoadEvent(openClose(objElement));
  20. });
  21. /* end multiple script load function */
  22.  
  23. /* start init function - design by Gez Lemon */
  24. function init()
  25. {
  26.   var objImage = document.getElementsByTagName('img');
  27.   var objHelp, objAnchor, objClone;
  28.  
  29.   for (var iCounter=0; iCounter<objImage.length; iCounter++)
  30.   {
  31.     if (objImage[iCounter].className == 'help')
  32.     {
  33.       objHelp = document.getElementById('container' + objImage[iCounter].id);
  34.       objAnchor = document.createElement('a');
  35.       objClone = objImage[iCounter].cloneNode(true);
  36.  
  37.       objAnchor.setAttribute('href', '#');
  38.       objAnchor.onclick = function() {return openClose(this);};
  39.       objAnchor.appendChild(objClone);
  40.       objHelp.style.display = 'none';
  41.  
  42.       objImage[iCounter].parentNode.replaceChild(objAnchor, objImage[iCounter]);
  43.     }
  44.   }
  45. }
  46. /* end function init() */
  47.  
  48. /* start openClose(objElement) function - design by Gez Lemon */
  49. function openClose(objElement)
  50. {
  51.   var objHelp = document.getElementById('container' + objElement.firstChild.id);
  52.  
  53.   if (objHelp)
  54.   {
  55.       if (objHelp.style.display == 'none')
  56.       {
  57.         objHelp.style.display = 'block';
  58.       }
  59.       else
  60.       {
  61.         objHelp.style.display = 'none';
  62.       }
  63.   }
  64.   return false;
  65. }
  66. /* end function openClose(objElement) */

top