Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a value from child window to text field in parent window

Tags:

javascript

I'm trying to pass and insert value from child window to the value attribute of text field which located in parent window, and not to its property. I've tried all the following but no luck so far:

Child window:

function getFile(oImg){
var id = <?php echo $id; ?>;
var editPage = window.opener.document;
oSrc = oImg.src;
lastSlash = oSrc.lastIndexOf('/');
oName = oSrc.substr(lastSlash+1);
var logo = 'logo'+id, logoHolder = 'logoHolder'+id;
//window.opener.document.getElementById(logo).value = oName;
//window.opener.document.getElementById(logo).setAttribute("value", oName);
//window.opener.document.getElementById(logo).innerHTML = oName;
//window.opener.document.getElementById(logo).setValue = oName;
window.opener.document.getElementById(logoHolder).src = "templates/img/user/" + oName;
this.close()
}

Parent page:

<input type="text" id="logo1" name="logo1" value="VALUE-SHOULD-BE-INSERTED-HERE" />

The first one just display the value in the text-field but when I'm trying to save it with jquery, the value is actually empty. Any idea?

like image 638
Simon Avatar asked Feb 16 '23 00:02

Simon


1 Answers

window.opener.document.getElementById(logo).value = fileName;

This should work—except that logo should be a string. Otherwise the opened child window will try to find a variable with the name logo, which it will probably not find.

So, do this:

window.opener.document.getElementById('logo').value = fileName;

No idea what you are doing wrong, maybe the PHP echo won’t output the correct ID? Anyway, it works fine for me. See this example:

parent.html

<input type="text" id="logo1" value="VALUE-SHOULD-BE-INSERTED-HERE" />
<script>window.open('child.html');</script>

child.html

<button id="btn">Click me</button>
<script>
btn.addEventListener('click', function () {
    var logo = 'logo' + '1';
    window.opener.document.getElementById(logo).value = 'something';
    window.close();
});
</script>
like image 131
poke Avatar answered Feb 17 '23 13:02

poke