I main php page in which there are 2 frames. Inside the second frame there is iframe. I want to access value of element on iframe document from the 1st frame.
I tried like this:
var frame1 = parent.frames[1];
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
var eleValue =frame2.contentWindow.document.getElementById("MyElement").value;
but I am not receiving any value though it is there.
var frame1 = parent.frames[1];
alert(frame1.name);
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
alert(frame2.name);
var txtClinic = frame1.document.getElementsByTagName('iframe')[0].contentDocument.getElementById("clinicFlag");
last line of code doesnot return any control object.
Here's a modified snippet of my answer linked in a comment. Function returns the
window
object of an (i)frame withid
passed (id
), ornull
, if the(i)frame
is not found. This method works only, if all the(i)frame
s are in the same domain. Alsoid
s given to(i)frame
elements must be unique throughout all documents involved in the window structure.This function can find a
frame
oriframe
with the passedid
regardless where it is placed in the window structure. Also the function can be placed and called in any window. I'd put this to the main page (as global). If the method is needed in subwindows, just call withtop.searchFrame(...)
.Instead of
id
s, alsoname
s can be used, as long as they are also unique. In that case theid
check insearchFrame()
needs to be edited toname
check.In your case, first you need to give an
id
to the targetiframe
, then you can use the code for example like this:The method above might be a bit overkilling to get a single reference, but it's very helpful, if you have to get these cross-window references multiple times within different windows.
The specific task of yours could be done like this:
name
s of the(i)frame
are also handy to use withframes
collection. Instead indices you can use names. Just always chain the reference starting from the main page, i.e. fromtop
. For example:Useful reading:
window.top
window.frameElement
window.frames