Mental Jetsam

By Peter Finch

Archive for April 9th, 2007

Finding a form element in a DIV using it’s name in Netscape 4

Posted by pcfinch on April 9, 2007

Netscape 4 is a little more complexity compared with the current (2004+) browser object models, as all HTML form fields that appear in divisions (DIV tags) actually exist in layers. i.e.

<div name="myDiv">
<form method="get" action="doit.jsp" name="myForm">
<input type="text" name="myInput">
</form>
</div>

A field in a DIV named myDiv will actually exist in the following location. The trick is that the layer object myDiv actually contains a seperate document that can be addressed just like any other document. It is also important to note that Netscape 4 does not allow forms to span divisions, so keep the form and the fields all in the say DIV tag.

obj = document.myDiv.document.myForm.myInput;

The following JavaScript will iterate over all the fields in all the DIV’s (layers) in the document and then iterate over all the forms and fields.

var s = "" ;
for(i = 0; i < document.layers.length; i++)
{
  s += "L" + i + ":" + document.layers[i].name + "(" ;
  l = document.layers[i] ;
  for(f = 0; f < l.document.forms.length; f++)
  {
    s += "F" + f + ":" + l.document.forms[f].name + "[" ;
    f = l.document.forms[f] ;
    for(e = 0; e < f.elements.length ; e++)
    {
      s += " E" + f + ":" + f.elements[e].name ;
    }
    s += "]"
  }
  s += ")" ;
}

Posted in HTML, javascript | Leave a Comment »