• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 24289
  • Last Modified:

parameter passing from child window to parent window

hi,

   my requirment is : I need to click a button on the parent html page. this should open a new child window which has two text fields and two buttons "Ok" and "Cancel".

when i click "Ok" the values in the textfields should be passed to the parent window and stored in two variables.

when i click "Cancel"  the window should be closed and the values inthe textfield if any should not be passed

  If any one has the idea please post the code.

waiting for reply.
0
bharat_ko
Asked:
bharat_ko
  • 4
  • 4
  • 4
  • +2
1 Solution
 
jkna_gunnCommented:

<input type="button" name="ok" value="Ok" onclick="passValues()">
<input type="button" name="cancel" value="cancel" onClick="window.close();">

<script language="JavaScript">
function passValues()
{
    // pass variable back to parent window
    window.opener.document.yourformname.yourinputfield = document.thischildform.textfield1; // adjust to your needs
 
}
</script>

you will need inputs set up on parent page to pass values back to in above example.
0
 
jarasaCommented:
You should close the window after clicking ok too

<input type="button" name="ok" value="Ok" onclick="passValues();window.close();">

Javier
0
 
jkna_gunnCommented:
true, but he never asked for that ;)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jarasaCommented:
I guess so :c)
0
 
bharat_koAuthor Commented:
hi jkna_gunn,

 i could not get u with the statment "you will need inputs set up on parent page to pass values back to in above example.".

 can u explain what you mean saying "input setup".

0
 
jkna_gunnCommented:
sorry what i meant was that inorder to pass information back you will need somewhere to pass them back to
for example an input field (most likely).

so in my code i had this
window.opener.document.yourformname.yourinputfield

so that means on your parent page, you would need a form called yourformname that had an input field called yourinputfield. of course im sure you have these already, so all you need to do is change the names to match your code.
0
 
bharat_koAuthor Commented:
hi jkna_gunn and jkna_gunn,

     can u clearly explain what is "inputs set up on parent page" .

  I think i need to stop execution of next statments on parent window till the child window is closed.


0
 
bharat_koAuthor Commented:
hi jkna_gunn and jarasa,

     can u clearly explain what is "inputs set up on parent page" .

  I think i need to stop execution of next statments on parent window till the child window is closed.


0
 
jarasaCommented:
Main page -> You have a Form on this page
<Form name=myForm>
<input type=text name=myField1 value="">
</Form>

Child page -> You change the value of a field inside the main page Form
window.opener.document.myForm.myField1.value = "Hello I write this from the Child Window";

You see now how it works??

Javier

0
 
jarasaCommented:
Don't accept my answer, OK? I'm just explaining you jkna_gunn Solution wich it the correct one.

Javier
0
 
mgeiserCommented:

try this...


=============================================
Parent Page

function openWindow() {

      //open dialog to get the name of the Template copy
      var url = escape(appRoot + "/activityTemplateModify.do?action=SelectTemplateCopyName");
      var features = "dialogWidth:400px;dialogHeight:225px;scroll:no;status:no;resizable:no;help:no;";

      var inputArgs = new Object();
      inputArgs.templateName = selectedTemplateName;
      inputArgs.templateId = selectedTemplateId;

      var retVal2 = window.window.showModalDialog(url, inputArgs, features);
      if (retVal2 != null) {
             if (retVal2.status == 1) {
                       alert(retVal2.text1)
                       alert(retVal2.text2)
         }
        }
}

=============================================
Cihild page

buttons:
<table class="dialogLayout" id="idCommand" cellpadding="0px" cellspacing="0px" width="100%">
<tr>
<td align="right">
      <input type="button" class="commonButtonH19W54" value="OK" onclick="closeWindow(true);" tabindex="2"/>
      <input type="button" class="commonButtonH19W54" value="Cancel" onclick="closeWindow(false);" tabindex="3"/>
</td>
</tr>
</table>


    function closeWindow(accept) {
        if (accept == true) {
      ret.status = 1;
      ret.text1 = document.getElementById("text1").value;
      ret.text2 = document.getElementById("text2").value;
      window.returnValue = ret
      window.close();
        }
        else {
                //the cancel button was clicked
      ret.status = 0;
      window.close();
        }
    }
0
 
bharat_koAuthor Commented:
thanks for your replies.

 i am getting a script error.

   it says "'ret' is undefined". so i declared 'ret' as

                     var ret = new Object();  in the child window (i.e) modal dialog.

but still i am getting the same error.

can u tell me why is it so.

one more thing. can i get the reference of my parent window variable or object to the child window (i.e) modal dialog. if  it can be done , can you write the code for that



0
 
jkna_gunnCommented:
// parent page
<script language="JavaScript">
  function openChild() {
       var win = window.open('child.html','Child','width=400,height=275,toolbar=yes,scrollbars=yes,resizeable=yes,menubar=yes,location=yes,status=yes');
   }
 </script>
 <form name="parentForm">
 <input type="text" name="textField1" value="r">
 <input type="text" name="textField2" value="r"><br />
 <input type="button" name="openchild" value="Open Child" onClick="openChild()">
 </form>


// child page
<script language="JavaScript">
  function passValues()
  {
      window.opener.document.parentForm.textField1.value = document.childForm.textField1.value;
      window.opener.document.parentForm.textField2.value = document.childForm.textField2.value;
  }
</script>
<form name="childForm">
<input type="text" name="textField1" value="">
<input type="text" name="textField2" value=""><br />
<input type="button" name="ok" value="OK" onClick="passValues();">
<input type="button" name="cancel" value="Cancel" onClick="window.close();">
</form>
0
 
aotyehelCommented:
I have tried to alter this last piece of code using radio buttons...but it seems to ignore whether or not the radio buttons are checked or not, so does not pass the values....or rather returns a value of 'undefined'.

As I am not a Javascript whizz....could someone alter the above code to enable a child window to pass through the value of a checked radio button value to one textfield.

This would greatly be appreciated.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now