Monday, January 14, 2013

entered value from child window to parent window

From a child window or a small window once opened, we can transfer any user entered value to main or parent window by using JavaScript. You can see the demo of this here. Here the parent window is known as opener. So the value we enter in a child window we can pass to main by using opener.document object model. So if the name of the form in parent window is f1 and the text box where we want the value to be passed is p_name ( in parent window ) then from the child window we can access them by using the object.

opener.document.f1.p_name

The value for this object can be assigned like this

opener.document.f1.p_name.value="Any value";

We will try to make it interactive so we will assign this to a value entered by the user. Then we will use one input box in child window and name it as c_name. So we can pass the value of the input box of child window to the parent window input box by this line.

opener.document.f1.p_name.value = document.frm.c_name.value;

Related Tutorial
Refreshing the parent window
Opening Window
We will keep this line inside a function and call this function on click of a button. Inside the function after executing the above line we will add the code to close the child window. Like this ..

opener.document.f1.p_name.value = document.frm.c_name.value;
self.close();

To open the child window this is the code used in parent window



Your Name
onClick=window.open("child3.html","Ratting",
"width=550,height=170,left=150,top=200,toolbar=1,status=1,");>Click here to open the child window


Inside the Child window code is here






(Type a title for your page here)










Your name

In the above code take care that there is no line break ( in parent file ) in line saying onClick=window.open(......

No comments:

Post a Comment