Tuesday, August 2, 2011

How to access embedded iframe's internal elements?

How do I access internal form elements of iframe which is embedded in an HTML page.

Ofcourse document.forms[0] will give me the first form in the parent document. But what I need is the form of the iframe.

Here is the solution

Example:
Test.html
==========
<html>
<head><title>Dynamic Content</title>
<script language="JavaScript">
 function testFun()

 {
    var doc = document.getElementById('myframe').contentWindow.document;
   alert(doc.forms[0].textbox.value);
 }

</script>
</head>
<body>
<iframe src="b.html" name="myframe"></iframe><BR>
<input type="button" onclick="testFun()" value="Submit"></input>
</body>
</html>

b.html
=======
<html>
<body>
<form name="aaa" action="aaa">
<input type=text name="textbox"></input>
<input type="submit"></input>
</form>
</body>
</html>

No comments:

Post a Comment