JavaScript: How to get value from nested form in iframe?

12. August 2004

Here is an example of how to get a value from a nested form in an iframe. It works in IE6, but not Netscape 7.1.

PageOne.htm
<html>
<head>
<script language='JavaScript'>

 function Search() {
  alert(document.frames("PageTwo").document.forms("Members").elements("Search").value);
 }

</script>
</head>
<body>
<input type='button' value='Search' onclick='Search();'>
<iframe name='PageTwo' src="PageTwo.htm" width='100%' height='100'>
</body>
</html>

PageTwo.htm
<html>
<body>
<form name='Members'>
    <input type='text' name='Search' value='Chris'>
</form>
</body>
</html>

General



Comments

Koen
Koen
5/12/2005 1:42:00 AM #
Any idea how to do this in http://asp.net" target="_blank">ASP.NET ?
Harry
Harry
10/19/2006 8:59:00 AM #
document.frames is not a function. pfft.
Harry
Harry
10/19/2006 9:17:00 AM #
what it SHOULD be is:

window.frames['PageTwo'].document.forms['Members'].elements['Search'].value
10/20/2006 7:27:00 AM #
My code above works perfectly in IE. You are correct in pointing out that document.frames is not a function in FireFox. So, if you want this to run fine in either web browswer, then you should use window.frames instead. Thanks for pointing that out!
10/20/2006 9:41:00 PM #
The code works brilliantly. Just wanted to add that the same code can be used to set values of a form inside an iframe. Thats what i realy needed and it worked flawlessly. Thanks
nreb10
nreb10
11/3/2006 5:56:00 PM #
i have problems in accessing some elements in an iframe.

my prob is i want to manipulate the color of the anchor (when active) in my iframe.

Can you help me how to do it?

this is my sample code:

window.frames['sample'].document.getElementById('test').focus()
Comments are closed