This sounds like a simple task but I'm finding the solution oddly hard to nail down.
All I want to do is populate a text input box in the parent window with data from an iframe, using a button click inside the iframe. And yes, both pages are on the same domain.
Tried various solutions with raw JS, Jquery, Ajax etc with no joy. I have found a solution to populate a <div>
container, but applying the same target id to a text input does not work.
I've also successfully used this Jquery script from the parent page to send data to the iframe, but can't work out how to do the reverse (iframe to parent)
<script>
$(document).ready(function(){
$('#submit').click(function(){
var iframe = document.getElementById('myiframe');
var doc = iframe.contentDocument || iframe.contentWindow.document;
var elem = document.getElementById('source');
doc.getElementsByName('target')[0].value = elem.value;
});
});
</script>
The simplified setup.
parent.html:
<!DOCTYPE html>
<html>
<body>
<iframe id="myiframe" frameborder="1" src="child.html"></iframe>
<form>
Iframe data populates here: <input type="text" name="target" id="target">
</form>
</body>
</html>
child.html:
<!DOCTYPE html>
<html>
<body>
<form>
Iframe data is input here: <input type="text" name="source" id="source">
<button type="button" onclick="functionToSendSourceToParent();">
</form>
</body>
</html>