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

Chris Tifer christ at saeweb.com
Tue Dec 16 08:09:12 CST 2003


> Well, the answer (unfortunately) depends a little on which browser you are
> using.  For example, for IE6+ derivatives, the following should work --
>
> var yellowTextArea =
> window.parent.top.document.frameYellow.document.frm1.textArea1.value;

It really shouldn't matter which browser is in use. As long as they a)
support frames and b) have JavaScript enabled you should always be able to
target another frame by using the following syntax:

parent.top.frames["frameName"]

So set that as a reference to a frame by doing:

var objFrame = parent.top.frames["frameName"]

Then if you want to reach a form on that page you do:

var objForm = objFrame.forms["formName"]

And if you want a handle to the element in question, you can:

var objEl = objForm.elements["elementName"]

Now you have 3 separate objects you can reference easily enough. If you want
to target more than one element, then you work off of your objForm object.



> Also, I've heard my ASP buddies mention that asp code doesn't necessarily
> follow exactly the same OOL heirarchy as Javascript (which is a very
> imperfect OOL, as far that goes), so don't expect the exact same reference
> schema to work the same in both Javascript and ASP.  Sorry.


I'm not sure what this means as ASP Objects are server-side. JavaScript
object are client-side. Two totally different scenarios here. There is no
window object in ASP. Nothing equivalent.  The server doesn't understand
cross-frame scripting. It doesn't even care about it.

Chris Tifer




More information about the Javascript mailing list