Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 663
raki Avatar asked Jul 04 '12 14:07

raki


People also ask

How do I get text in an iframe?

Assuming your iFrame is in the same domain as your OutSystems application you can run Javascript to get the values: var myIFrame = document. getElementById('<Widget.Id>'); //Fetch Inner HTML inside body var contentHTML = myIFrame. contentWindow.

Can an iframe access its parent?

window); When a page is running inside of an iframe, the parent object is different than the window object. You can still access parent from within an iframe even though you can't access anything useful on it.

Which function is used to get value from the input textbox?

We can get the value of the text input field using various methods in JavaScript. There is a text value property that can set and return the value of the value attribute of a text field. Also, we can use the jquery val() method inside the script to get or set the value of the text input field.

Can iframe access Windows?

open or a window inside <iframe> , and that window comes from the same origin, then we have full access to that window. otherwise, if it comes from another origin, then we can't access the content of that window: variables, document, anything.


1 Answers

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.

like image 197
Niclas Larsson Avatar answered Sep 22 '22 16:09

Niclas Larsson