简体   繁体   中英

Get value of a text box in iframe from parent window

I have an iframe in my page and how can i get a the value of t text box in the frame from my parent page on a button click?

here is my code

<div>
<iframe src="test.html" >
<input type=text id="parent_text">
<input type="button">
</div>

here is d test.html

<input type="text" id="frame_text">

thanks

Something like this:

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var input = innerDoc.getElementById('frame_text');

First you get the the iframe. Then you get the first valid dom document from inside the iframe.

And finaly get the input box.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function getIframeText() {
            var iframe0 = document.getElementById("iframe0");
            var iframe0document=iframe0.contentDocument||iframe0.contentWindow.document;
            var inputIframe = iframe0document.getElementById("frame_text");
            alert(inputIframe.value);
        }
    </script>
</head>
<body>
    <div>
        <button onclick="getIframeText()">get iframe text</button>
        <iframe id="iframe0"  src="test.html" >
        <input type=text id="parent_text">
    </div>
</body>
</html>

If you have Div/ input as follows in one Iframe

 <iframe id="ifrmReportViewer" name="ifrmReportViewer" frameborder="0" width="980"

     <div id="EndLetterSequenceNoToShow" runat="server"> 11441551 </div> Or

     <input id="txtSequence" type="text" value="10500101" />

     <form id="form1" runat="server">        
       <div style="clear: both; width: 998px; margin: 0 auto;" id="divInnerForm">          

            Some Text

        </div>
     </form>
 </iframe>

Then you can find the text of those Div using the following code

var iContentBody = $("#ifrmReportViewer").contents().find("body");
var endLetterSequenceNo = iContentBody.find("#EndLetterSequenceNoToShow").text();

var txtSequence = iContentBody.find("#txtSequence").val();
var divInnerFormText = iContentBody.find("#divInnerForm").text();

I hope this will help someone.

$('iframe').contents().find('#mytextbox').val("myvalue");

Here is parent page code (I mean main page call to iframe ):

<script>
 function frameClick(string){
   var name=string;
   document.getElementById("parent_text")=name;
   }
</script>

<div>
<iframe src="test.html" id="frame1">
<input type=text id="parent_text">
</div>

Here is iframe code(test.html):

<form method="post" action="" id='frm1' >
<input type="text" id="frame_text">
<input type="button" id="btm1" name="hangup" onclick="parent.frameClick(this.form.frame_text.value);" value="submit">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM