Link to home
Start Free TrialLog in
Avatar of bharat_ko
bharat_ko

asked on

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.
Avatar of jkna_gunn
jkna_gunn


<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.
You should close the window after clicking ok too

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

Javier
true, but he never asked for that ;)
I guess so :c)
Avatar of bharat_ko

ASKER

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

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


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.


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

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

Javier
ASKER CERTIFIED SOLUTION
Avatar of mgeiser
mgeiser

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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



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