I need to populate a text input field on the parent page from a button click inside an iframe (same domain). I've been using the following to accomplish the opposite (parent to iframe) but I'm not sure how to reverse the process. Any help would be appreciated.
parent.html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
var iframe = document.getElementById('myiframe');
var doc = iframe.contentDocument || iframe.contentWindow.document;
var elem = document.getElementById('username');
doc.getElementsByName('user')[0].value = elem.value;
});
});
</script>
<body>
<input type="text" id="username">
<input type="submit" id="submit">
<iframe id="myiframe" frameborder="1" src="child.html"></iframe>
</body>
child.html:
<body>
<input type="text" name="user" id="user">
</body>