Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Child window updating parent window 4

Status
Not open for further replies.

OBang

Programmer
Jan 13, 2006
4
0
0
US
I have a child form that updates a parent form.
The field on the parent form is called "t_1".
I am using the following javascript to update its value:
opener.document.form2.t_1.value=TaskId;

This works fine the value of the variable TaskId is filled in fine. But what I really need is to replace the t_1 in the javascript line with a variable field name, so that I can update fields such as t_2, t_15, t_22 etc.

I am a VB.Net programmer, with limited javascript skills looking for the correct syntax to make it happen.

I have a variable called tcount that should take the place of the t_1 field name. Here is the variable declared:
var tcount= 't_' + QueryString('tcount');

So in theory I want the statement, opener.document.form2.tcount.value=TaskId;
But that is the wrong syntax and causes an error.

Any ideas?
 
Have you tried just:

window.opener.tcount = TaskId

variables are properties of the window object.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hi Tracy,

Thank you for the suggestion. I tried it out as well as a few other variations but without any luck. Just excluding the '.value' at the end eliminates page errors but nothing gets updated on the parent window.

Again the statement that works is: window.opener.document.form2.t_1.value = TaskId;
But the t_1 must be replaced by variable holding the field name.

Thanks,

Odd Bang
 
Given that my application is a VB.Net application, I decided to create the Javascript I needed dynamically during the post back event of the child form.

I added the following code to my 'OK' button:
======================================================
Code:
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim tCount As String
        tCount = "t_" + Me.Request.QueryString.Item("tCount")
        Dim TaskId As String
        TaskId = Me.txtTaskId.Text
        Response.Write("<script language='javascript'> { window.opener.document.form2." + tCount + ".value = '" + TaskId + "'; }</script>")
        Response.Write("<script language='javascript'> { self.close() }</script>")
    End Sub
========================================================
This resolves my issue.

Thanks for all suggestions!

Odd Bang
 
I must have missed where you mentioned that the variable you wanted to update was really a form field. What you used is proper for that. You can use varible values in the statement by using form elements collection directly:
Code:
var t_1 = "aFormField";
window.opener.document.form2.elements[t_1].value = TaskId;
this will access a field named aFormField in the form form2.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thank you Tracy, that worked great exactly what I was looking for.

Odd Bang
 
Unfortunately, people like the convenience of using the dot notation to access form elements:

[tt] document.formname.elementname.value[/tt]

and never learn how to use the much more flexible standard method:

[tt] document.forms[formname].elements[elementname].value[/tt]

The latter allows you to use variables, literals, or a concatenation of both. You can even use a function call that returns a string if you find the need. Just remember that forms and elements are collections, with all that entails (including the ability to enumerate them).

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I have a similar issue with child and parent windows:

I want to - via the child - reload the parent that has form fields, and preserve the values entered into the fields. The code below does simliar to that except (for testing purposes) prepends "hi" to the text field's data.

As intended, it will reload the page.
Then (in Firefox) it will insert the textbox "hi" + text and, after a half second, clear the textbox (or is it reloading the page again?).
In IE it doesn't appear to display the hi text at all.. It just clears the textbox.

Here is the parent code:

Code:
<html>
<head>
 <title>test</title>
 <script language="JavaScript">

function launch(newURL)
{
  var remote = open(newURL, "myRemote", "height=100,width=300");
  if (remote.opener == null) remote.opener = window;
  return remote;
}

function popup(pageurl)
{
  myRemote = launch(pageurl);
}
 </script>
</head>

<FORM ACTION="" NAME="sendmessage">
 Message:<BR>
 <TEXTAREA NAME="message" COLS="80" ROWS="10" WRAP="PHYSICAL"></TEXTAREA>
 <BR>
 <input name="Submit" type="submit">
</FORM>

<BR>
<A HREF="javascript:popup('popup.html')">do the JS stuff</A>


</body>
</html>


And here is the child code (popup.html):
Code:
<HEAD>
 <TITLE>Add Item</TITLE>
<SCRIPT TYPE="text/javascript">
<!--
  openerform=window.opener.document.sendmessage;
  window.opener.location.href=window.opener.location.href;
  window.opener.document.sendmessage.message.value="hi" + openerform.message.value;
//-->
</SCRIPT>
</HEAD>

<BODY>
Done.
</BODY>

</HTML>

What does it appear I am doing wrong?

Thanks - Jim
 
Get the value first then refresh then rewrite.
[tt]
var openerformmessagevalue=window.opener.document.sendmessage.message.value;
window.opener.location.href=window.opener.location.href;
window.opener.document.sendmessage.message.value="hi " + openerformmessagevalue;
[/tt]
 
Thanks. That is basically what I did with openerform, saving the whole form object, sendmessage. In any event, I tried your code and it yielded the same results as mine.

Thanks - Jim
 
Give it some time delay on the 3rd line with setTimeout.
[tt]
setTimeout("window.opener.document.sendmessage.message.value='hi '"+openformmessagevalue+";",1000);
[/tt]
 
Thanks again. Alas, unfortunately, it didn't work. I tried increasing the delay, but to no avail.

Jim
 
This is the elaboration resolving the timing rather than adhoc.
[tt]
<SCRIPT TYPE="text/javascript">
<!--
var smsg=window.opener.document.sendmessage.message.value;
doit(smsg);

function doit(s) {
if (window.opener && window.opener.document && window.opener.document.sendmessage) {
window.opener.document.sendmessage.message.value="hi " + s;
} else {
setTimeout("doit("+s+")",200);
}
}
//-->
</SCRIPT>
[/tt]
 
You ARE the Man! Unless you're a girl, in which case you ARE the Woman!

Thanks!
Jim
 
Hold it... I got too excited too quickly. You are still definitely the [Wo]Man, because you've been so patient and persistent. But I realized as I used the JS in the pages I needed it, that it doesn't reload the parent page. And when I added the window.opener.location.href=window.opener.location.href code back in there, before calling doit(smsg), it was more of the same. The refreshing of the parent is necessary because it's in the popup that you would update a <select> dropdown menu on the parent page. But the data the user puts into the other form fields must be preserved.

Perhaps I chosen the wrong methodology in forcing a reload? It looks like I found JS code at that will update the dropdown without a reload. I don't know if that's the best way to do it (for instance, I would want it alphabetically sorted by dropdown option, not appended to the end). But now that I have brought up the question of preserving values after reload, I think it is worth perfecting for the pursuit of (my) knowledge.

Thanks - Jim
 
Yes, I just forgot to paste the line in as well. It will do just as good, will it not?
[tt]
var smsg=window.opener.document.sendmessage.message.value;
window.opener.location.href=window.opener.location.href;
doit(smsg);
[/tt]
href or no href would do the same too.
 
Dear Members (tsuji)
i have similar/same question with the child window passing a value back to the parent window. I have read this thread and still can't figure it out. Is it possible to describe my specific case/problem for help?

Thank you
 
That's what we're here for!

If you want to investigate things posted here and what they mean, then post here.

If you just want to describe your particular experience and post code of yours that's baffling you, why not start a new thread...

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
all the expert answer questions in this threat. Please have a look in my new started thread. i am desperat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top