Focus first form field

From Noah.org
Revision as of 02:41, 17 April 2008 by Root (talk | contribs) (New page: Category:Engineering This focuses input on the first non-hidden text or textarea field on the first form. This is short and gets the job done. <pre> ... <script language="JavaScript"...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This focuses input on the first non-hidden text or textarea field on the first form. This is short and gets the job done.

...
  <script language="JavaScript">
  // focus first non-hidden text form field.
  function focus_first ()
  {
      var form = document.forms[0];
      if (form != null && form.elements[0] != null)
      {
          for (var i = 0; i < form.elements.length; ++i)
          {
              var field = form.elements[i];
              if (field.type!="hidden" && (field.type=="text" || field.type=="textarea"))
              {
                  field.focus();
                  break;
              }
          }
      }
  }
  </script>
  </head>
  <body onload="focus_first()">
...