/    Sign up×
Community /Pin to ProfileBookmark

Help on passing value from Parent window to Child

i created a popup window and i want to pass just a single value in my form to the child window ..

any idea how to do it?

my coding is like this

—- PARENT WINDOW —-
function open_win(){

window.open(‘calculate.php’, ”, ‘left = 103, top = 72, width=400,height=300,scrollbars=no,menubar=no,location=no,status=yes,resizable=yes,toolbars=no’);

}

<form name=”jobprofile” action=”” method=”post”>
<input type=”text” name=”txtsupervisor” value=””>
<input type=”button” onclick=”open_win();” value=”Browse”>
</form>

— child window / calculate.php —

function passvariable(){
window.opener.jobprofile.txtsupervisor.value = supervisorform.txtsupervisor.value;
window.close();
}

<FORM METHOD=POST ACTION=”” name=”supervisorform”>
<INPUT TYPE=”text” NAME=”txtsupervisor” value=””>
<INPUT TYPE=”button” name=”submit” value=”submit” onclick=”passvariable()”>
</FORM>

……………….

i can pass value from child window to parent window but i wonder how to pass value from parent window to child window

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@fredmvDec 30.2003 — If you create your window like this:var w = window.open('',...You can access elements in the child like this (for example, a form element):w.document.foo.bar.value = 'foo';[b][color=red]Edited:[/color][/b] Fixed missing object.
Copy linkTweet thisAlerts:
@ltingauthorDec 30.2003 — w.foo.bar.value = 'foo';

then i still have to set the 'foo' manually ...

i want

w.foo.bar.value = whatever i put in the text value in parent window


something like this e.g?

w.foo.bar.value = form.name.value

???
Copy linkTweet thisAlerts:
@fredmvDec 30.2003 — My previous example will allow you to do that. Simply use the same notation (or different notation, such as arrays) for accessing the form elements, but replace [font=courier]foo[/font] with the name of your form, and [font=courier]bar[/font] with the name of the form element you want to access.
Copy linkTweet thisAlerts:
@ltingauthorDec 30.2003 — var w = window.open('calculate.php', null, 'left = 103, top = 72, width=400,height=300,scrollbars=no,menubar=no,location=no,status=yes,resizable=yes,toolbars=no');

w.supervisorform.txtsupervisor.value = jobprofile.txtsupervisor.value;


it doesn't work at all ....


w.supervisorform.txt.supervisor.value is the form element in my childwindow ..

jobprofile.txtsupervisor.value is the form element in my parent window..


what i want is when i press a button a POP-UP window appear..

then what is in my jobprofile.txtsupervisor.value will be pass into form element supervisorform.txtsupervisor.value in child window....
Copy linkTweet thisAlerts:
@fredmvDec 30.2003 — Remove the [font=courier]var[/font] unless it's inside of a function so the variable still exists after you create the window and it should work correctly assuming your form and element names are correct.
Copy linkTweet thisAlerts:
@ltingauthorDec 30.2003 — <HTML>

<head>

<script type="text/javascript">

function open_win(){

w = window.open('calculate.php', null, 'left = 103, top = 72, width=400,height=300,scrollbars=no,menubar=no,location=no,status=yes,resizable=yes,toolbars=no');

w.supervisorform.txtsupervisor.value = jobprofile.txtsupervisor.value;

}

</script>

</head>

<BODY>

<FORM name="jobprofile" method="POST" action="">

<TABLE>

<TR>

<TD>

<DIV align="right">Supervisor : </div>

</TD>

<TD colspan="2">

<input type="text" name="txtsupervisor" size="50" maxlength="50" value="">

</TD>

<TD colspan="2">

<input type="button" onclick="open_win();" value="Browse">

</TD>

</TR>

</TABLE>

</FORM>

</BODY>

</HTML>


this is my child window calculate.php

<HTML>

<HEAD>

<TITLE> AeraSoft - SmartHR </TITLE>

<SCRIPT LANGUAGE="JavaScript">

<!--

function passvariable()

{

window.opener.jobprofile.txtsupervisor.value= supervisorform.txtsupervisor.value;

window.close();

}

//-->

</SCRIPT>

</HEAD>

<BODY>

<FORM METHOD="POST" ACTION="" name="supervisorform">

<TABLE>

<TR>

<TD>

<INPUT TYPE="text" NAME="txtsupervisor" value="">

</TD>

<TD>

<INPUT TYPE="button" name="submit" value="submit" onclick="passvariable()">

</TD>

</TR>

</TABLE>

</FORM>

</BODY>

</HTML>



i removed the VAR .. it still doesn't work
Copy linkTweet thisAlerts:
@fredmvDec 30.2003 — I was assuming you were using it inside of an event handler as opposed to a function (using only the JavaScript inside the event handler as opposed to otherwise embedded or external). I've spotted your problem, you aren't accessing the form element correctly through the DOM (not your fault, but rather my fault accidently omitting in it in my original example). It should look like this:w.supervisorform.txtsupervisor.value = jobprofile.txtsupervisor.value;Should be this:w.document.supervisorform.txtsupervisor.value = document.jobprofile.txtsupervisor.value;Note the addition of [font=courier]document[/font]. ?
Copy linkTweet thisAlerts:
@ltingauthorDec 30.2003 — ?

still not working ..

i wonder what is happening on my coding ...


i already change to

w.document.supervisorform.txtsupervisor.value = document.jobprofile.txtsupervisor.value;

but when i press submit ..

the child window still didn't show anything
Copy linkTweet thisAlerts:
@fredmvDec 30.2003 — Did you get any JavaScript errors?
Copy linkTweet thisAlerts:
@ltingauthorDec 30.2003 — when i press submit..

the status bar at the parent window written errors on page

but the pop up window still appear with everything looks great ..

i still can enter something into the child window and pass back to parent window with no errors..
Copy linkTweet thisAlerts:
@fredmvDec 30.2003 — I'm guessing you have a typo somewhere, because after breifly looking over that syntax, it appears to be correct.
Copy linkTweet thisAlerts:
@ltingauthorDec 30.2003 — oh yupp ...

forget to tell ..

both my windows are saved under .php

is there any problem with it?
Copy linkTweet thisAlerts:
@fredmvDec 30.2003 — The extension shouldn't matter. Look around for a typo on the reference to the form or the name of the form, it has to be something related to that
Copy linkTweet thisAlerts:
@ltingauthorDec 30.2003 — i discover a new error ...


when i typed something and pressed the submit button, the parent window didn't pass anything to the child window..

so i went to the child window i input something, pressed the button. the value is passed to the parent window with no errors..

again i tried to pass something back to parent window.. this time it succeed ... weird ..

seem like i must pass something back from child window first then only i can pass something back from parent to child
×

Success!

Help @lting spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...