Javascript: Send / Pass value of child window to parent window

Posted: January 17, 2008 in Javascript
Tags: , , ,

 

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.

About these ads
Comments
  1. Kevin says:

    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!

  2. hspinfo says:

    Thanks Kevin,

    For your suggestion. I have changed the background color of my blog.

  3. Naren says:

    Thank you……

  4. jordan says:

    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.

  5. 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

  6. hspinfo says:

    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;”

  7. hi hspinfo,

    Thanks for sharing your knowledge. I found it great working for me.

    Thanks again

  8. cool stuff… getting to know javascript better thanks to helpful tuts like yours… thanks

  9. Anil says:

    I found it useful and how about including a modal widow stuff with some code samples. I was also looking for that as well.

  10. bob says:

    6Uufeq hi nice site thx http://peace.com

  11. bhanu says:

    Good and use ful

  12. Dan says:

    Hi,

    Many thanks for this. Very helpful.

  13. Epy says:

    Thank you so much…. I been searching for this for while.

  14. KC says:

    This is also exactly what Im looking for.. Many many many thanks…^^

  15. manikkuttan says:

    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

  16. gundholmu says:

    finally….
    Thank you so much…. I been searching for this for while.

  17. nikisinten says:

    thank you,,

    but i have a question : how about send/pass multivalue from child window’s to parent window’s??

    help me please…

  18. saigopal says:

    nice article thank you very much……

  19. Azhar says:

    Very Good Post, Thanks for sharing this stuff.

  20. Sohail says:

    Simply excellent information shared in easy-to-understand manner. Thank you

  21. Shyam says:

    It is really helpfull . I was looking for the same.

  22. iwansyah says:

    it’s very simply
    i like it

    thank’s for the tutorial
    it’s very helpful

    once again thank’s

  23. Justin says:

    Thanks for your sharing :)

  24. meer says:

    i get an error message
    window.opener is null
    can anyone help?

  25. shahbaz says:

    i want to do the same thing for radio button instead of text box,

  26. Márcio Simão says:

    Nice post!

    Anybody knows a way to do the same thing using a modal window wity JQuery?

  27. Nice dear its very useful….

  28. Sushma says:

    need a js to pass parameter from parent to child window

  29. pankajbhirud says:

    Hi,

    I want get multiple value from check box in text …(from open child window to parent window)

  30. Ghulam Mustafa says:

    Great work thank a lot for helping me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s