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

adding listbox items from popup window

Hi
   whats wronfg with this code, When I click on the child window I need to add new Item to the listbox in the parent window, but not happening!!

thanks in advance
Sree

parent.htm
-------------
<script language="javascript">
    function openIt(){
         window.open("child.htm");
    }
</script>
<form>
<select name="selectbox">
    <option value="Y">Yes</option>
    <option value="N" selected>No</option>
</select>
<input type="button" onclick="openIt();">
</form>


child.htm
----------

<Script LANGUAGE="JavaScript">
<!--
var numItems=2;
function setIt() {
           var txt = "sree";
     var addOption = new Option(txt,txt);
        window.opener.document.forms[0].selectbox.options[window.opener.document.forms[0].selectbox.length] = addOption;
         window.opener.document.forms[0].selectbox.selectedIndex=0;

//-->
</script>
<form>
<input type="text" name="inputbox" class="txt" value="100">

<input type="submit" name="btnUpdate" value="Save" onClick="setIt();">
</form>
 
0
msreekm
Asked:
msreekm
2 Solutions
 
sybeCommented:
1. you are missing the cloing "}" on the function setIt().


2.
You should put the function that creates the additional option in the "opener" document. Then call that function from the opener. At least in Internet Explorer.

The trouble is that if you create the "new Option" in the child window, then the new created option will be lost when you close the child window (because it is defined in the child window).
Netscape does not have this problem.

So put this function in the parent.htm
<script>
function setIt(mytext) {
    var txt = mytext;
    var addOption = new Option(txt,txt);
    document.forms[0].selectbox.options[document.forms[0].selectbox.length] = addOption;
        document.forms[0].selectbox.selectedIndex=0;
}
</script>

And call it from the child window with

<script>
opener.setIt('sometext');
</script>
0
 
fritz_the_blankCommented:
I have also seen problems in IE where you can not manipulate controls on the parent via code from the child. It is best to pass parameters from the child to the parent and run the code there:

**********
parent.htm
**********

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function addOption(strDisplay,strValue){
     var objForm = document.forms[0]
     var objFormField = objForm.elements['select1']
     var intLength = objFormField.options.length;
     objFormField.options[intLength] = new Option(strDisplay,strValue);

}
function openChild(){
     var winChild = window.open('child.htm','winChild','width=150,height=150');
}
//-->
</SCRIPT>

</HEAD>
<BODY onLoad=openChild()>
<FORM action="" method=POST id=form1 name=form1>
<SELECT id=select1 name=select1>
     <OPTION></OPTION>
</SELECT><p>
</FORM>
</BODY>
</HTML>


***********
child.htm
***********

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
Display<INPUT type="text" id=text1 name=strDisplay><br>
Value<INPUT type="text" id=text2 name=strValue><br>
<INPUT type="button" value="Button" id=button1 name=button1 onClick="window.opener.addOption(this.form.strDisplay.value,this.form.strValue.value)">

</FORM>

</BODY>
</HTML>


Fritz the Blank
0
 
jaysolomonCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: sybe {http:#8185833} & fritz_the_blank {http:#8186424}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now