Difference between revisions of "Focus first form field"

From Noah.org
Jump to navigationJump to search
(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"...)
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Engineering]]
 
[[Category:Engineering]]
 +
[[Category:Javascript]]
 
This focuses input on the first non-hidden text or textarea field on the first form. This is short and gets the job done.
 
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>
 
<pre>
 
...
 
...
  <script language="JavaScript">
+
<script type="text/javascript">
  // focus first non-hidden text form field.
+
// This will focus first non-hidden text or textarea field on the first form.
  function focus_first ()
+
function focus_first ()
  {
+
{
      var form = document.forms[0];
+
    var form = document.forms[0];
      if (form != null && form.elements[0] != null)
+
    if (form != null && form.elements[0] != null)
      {
+
    {
          for (var i = 0; i < form.elements.length; ++i)
+
        for (var i = 0; i < form.elements.length; ++i)
          {
+
        {
              var field = form.elements[i];
+
            var field = form.elements[i];
              if (field.type!="hidden" && (field.type=="text" || field.type=="textarea"))
+
            if (field.type!="hidden" && (field.type=="text" || field.type=="textarea"))
              {
+
            {
                  field.focus();
+
                field.focus();
                  break;
+
                break;
              }
+
            }
          }
+
        }
      }
+
    }
  }
+
}
  </script>
+
</script>
  </head>
+
</head>
  <body onload="focus_first()">
+
<body onload="focus_first()">
 
...
 
...
 
</pre>
 
</pre>

Latest revision as of 03:28, 17 April 2008

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 type="text/javascript">
// This will focus first non-hidden text or textarea field on the first form.
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()">
...