Mental Jetsam

By Peter Finch

  •  

    April 2007
    M T W T F S S
    « Mar   May »
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30  
  • My del.icio.us links

  • Flickr Photos

    S5001187

    S5001169

    S5001136

    More Photos
  • Wordpress Stuff

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 += ")" ;
}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>