[Javascript] how do you access a form element from another frame ?

David Lovering dlovering at gazos.com
Fri Dec 5 09:27:42 CST 2003


Generally, the following scheme will work across an assortment of common
browsers (I tested it with IE6+, and the W3C specs support it).  I'm sure
some of the syntax Nazis will get on my case, but here goes:

var greenDoc = window.parent.top.document.frames['frameGreen'].document;
var blueDoc = window.parent.top.document.frames['frameBlue'].document;
var yellowDoc = window.parent.top.document.frames['frameYellow'].document;
var redDoc = window.parent.top.document.frames['frameRed'].document.

This of course assumes that you are within the contents of one of these
forms (and not a remote child window), and that they are all spawned from
the same top-level frameset.  Even more bizarre configurations are easy to
manage with some slight tweaking.

For example, if you were inside frameGreen, and you wanted to access field
"myIndex" inside form "form1" which is contained within frameYellow,
something like this might work:

myIndexValue = yellowDoc.form1.myIndex.value;

The only caveat is that some instances may require that you use an
eval("stuff") instead of the core declarations I've included above.  The
rule for requiring or not requiring this work-around is not absolutely
consistant with the W3C spec (at least insofar as I've tested it with IE6+).
I'm sure someone more versed in the nuances of the "real" implementation of
the W3C specs will now step forward to enlighten me/us on the subject.

Let me know if this works for you.

-- Dave Lovering

----- Original Message ----- 
From: "DEV" <dev at qroute.net>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Thursday, December 04, 2003 9:25 PM
Subject: [Javascript] how do you access a form element from another frame ?


> Here is the situation;
>
>
> _______________________
>
>          ( frameGreen )
> _______________________
>                            |
>                            |
>                            |
> ( frameBlue )        | ( frameYellow )
>                            |
>                            |
>                            |
> _______________________
>
>          ( frameRed )
> _______________________
>
>
> frameYellow has a form named frm1 and a textarea called textArea1
>
> how does a button on the frameBlue update the value in the textArea1
>
> I tried the following, all failed.
>
> onClick="top.frameYellow.document.frm1.textArea1.value='X'"
>
> onClick="top.frameYellow.window.document.frm1.textArea1.value='X'"
>
> onClick="parent.frameYellow.document.frm1.textArea1.value='X'"
>
>
>
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>





More information about the Javascript mailing list