Sending child window’s value to parent window is one of the very common requirements for web developers. I also used it a lot, so decided to make a post for some of our beginners friends to Javascript. There are many ways you can send values of child window to parent window, however, I have described here two ways to send value from child window to parent.
Either you can send value through an argument to a function or you can directly set parent window’s object value from the client window. How to do it? that I have described below.
Find the below code to send value to parent window through an argument to a function. Write below code in your parent window.
<head>
<script language=”javascript” type=”text/javascript”>
function GetValueFromChild(myVal)
{
document.getElementById(‘mytxt’).value = myVal;
}
</script>
</head>
<body>
<input id=”mytxt” type=”text” />
</body>
Fig. 1
Here in fig 1, “mytxt” is a textbox object whose value will be received from the child window. “myVal” is argument of this function, this function will be called from the child window later.
Find the below code to be written in child window.
<head>
<script language=”javascript” type=”text/javascript”>
function SendValueToParent()
{
var myVal = document.getElementById(‘mytxt’).value;
window.opener.GetValueFromChild(myVal);
window.close();
return false;
}
</script>
</head>
<body>
Type text here and send it to parent window:
<input id=”mytxt” type=”text” />
<input id=”btn1″ type=”button” value=”Send Value to Parent” onclick=”javascript:return SendValueToParent();” />
</body>
Fig. 2
Here in fig 2, we are sending child window’s textbox value to the parent window’s function by calling “GetValueFromChild(myVal)”. “myVal” is the value of the child window’s textbox and will be sent to parent window as an argument by clicking “btn1” button.
You can also set parent window’s object value from the child window. You only need to do is, just write the below code in your child window only and there is no need to write any script in the parent window. Only you need is a textbox or any other object that can display the value of the parent window in browser.
Put the following code in child window to set parent window’s object value:
<head>
<script language=”javascript” type=”text/javascript”>
function SetValueInParent()
{
window.opener.document.form1.mytxt.value = document.getElementById(‘mytxt1’).value;
window.close();
return false;
}
</script>
</head>
<body>
Type text here and set it to parent window :
<input id=”mytxt1″ type=”text” />
<input id=”btn1″ type=”button” value=”Set Value in Parent” onclick=”javascript:return SetValueInParent();” />
</body>
Fig. 3
In fig 3, we have set the value of child window’s textbox object to parent window’s textbox object directly by using it’s ID property. I have called this function by clicking on “btn1” button.
Whether you found this article useful or not? please provide your valuable comments.
Thanks for sharing that info – I have not tried it yet but I will check it out. The black background on your blog makes it kinda difficult to read the colored code – maybe lighten the background to a less dark greyish?? Anyway, thanks!
Thanks Kevin,
For your suggestion. I have changed the background color of my blog.
Thank you……
this is exactly what i am looking for. only question: how would i modify this to send back, say, two strings? could this be used to send back mytxt and mytxt2, for instance? thanks.
thanks a lot for helping me out . I was looking for this tip for months.
can some one tell how can i read title of a child window in parent
again thanks a lot
Hi Pushpendra,
Before closing the child window, you can pass the following thing as a new parameter to the parent window, this will return the title of that child window:
“window.document.title;”
hi hspinfo,
Thanks for sharing your knowledge. I found it great working for me.
Thanks again
cool stuff… getting to know javascript better thanks to helpful tuts like yours… thanks
I found it useful and how about including a modal widow stuff with some code samples. I was also looking for that as well.
6Uufeq hi nice site thx http://peace.com
Good and use ful
Hi,
Many thanks for this. Very helpful.
Thank you so much…. I been searching for this for while.
This is also exactly what Im looking for.. Many many many thanks…^^
window.opener is no working for me…..Error: window.opener.GetValueFromChild is not a function
Source File: http://localhost/chat/video.php?x=uuuu&y=yayayayaya
Line: 8 it is saying like that….I have valid user. click me.………..I am trying to pass url of popup window to parent window..Please help me
finally….
Thank you so much…. I been searching for this for while.
thank you,,
but i have a question : how about send/pass multivalue from child window’s to parent window’s??
help me please…
nice article thank you very much……
Very Good Post, Thanks for sharing this stuff.
Simply excellent information shared in easy-to-understand manner. Thank you
It is really helpfull . I was looking for the same.
it’s very simply
i like it
thank’s for the tutorial
it’s very helpful
once again thank’s
Thanks for your sharing 🙂
i get an error message
window.opener is null
can anyone help?
i want to do the same thing for radio button instead of text box,
Nice post!
Anybody knows a way to do the same thing using a modal window wity JQuery?
Nice dear its very useful….
need a js to pass parameter from parent to child window
Hi,
I want get multiple value from check box in text …(from open child window to parent window)
Great work thank a lot for helping me