?
Solved

adding listbox items from popup window

Posted on 2003-03-21
4
Medium Priority
?
1,058 Views
Last Modified: 2007-12-19
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
Comment
Question by:msreekm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 28

Accepted Solution

by:
sybe earned 152 total points
ID: 8185833
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
 
LVL 46

Assisted Solution

by:fritz_the_blank
fritz_the_blank earned 148 total points
ID: 8186424
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
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10149480
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question