Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 15th, 2005, 01:24 AM
Registered User
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default adding new listbox object to window opener


Can anyone help me:

- My main window has an empty listbox. Also this main window has a link to pop up a new window(Like in yahoo, when do you want to try to inserting an address from address box).
This popup window will populate the value of the listbox in main window.

The code to populate the listbox is:

This code is in Popup window

this.opener.document.mainForm.myListBox.option[this.opener.document.mainForm.myListBox.length] =
new Option("NEW VALUE", "NEW NAME", false, false)

But The browser IE return error.

Does anyone know why and help me?

Old January 15th, 2005, 04:38 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik

<SCRIPT language="JavaScript">

   function addOption(optionsArray, insertAtIndex, optionText, optionValue)
      var indexCounter = optionsArray.length;

      optionsArray[indexCounter] = new Option();

      for (; indexCounter >= insertAtIndex; indexCounter--)
         optionsArray[indexCounter].text = optionsArray[indexCounter - 1].text;
         optionsArray[indexCounter].value = optionsArray[indexCounter -1].value;
         optionsArray[indexCounter].defaultSelected = optionsArray[indexCounter - 1].defaultSelected;

      var myNewOption = new Option(optionText,optionValue);
      optionsArray[insertAtIndex] = myNewOption;


   function cmdAddOption_onclick()
      var form = document.myForm;
      var insertAtIndex = form.mySelectControl.selectedIndex + 1;
      var optionText = form.txtOptionText.value;
      var optionValue = form.txtOptionValue.value;
      var optionsArray = form.mySelectControl.options;
      addOption(optionsArray, insertAtIndex, optionText, optionValue);

   <FORM NAME="myForm">
      New Option Text:
      <INPUT TYPE="text" NAME=txtOptionText>
      New Option Value:
      <INPUT TYPE="text" NAME=txtOptionValue>
      <INPUT TYPE="button" 
         VALUE="Add New Option" 
      <SELECT NAME=mySelectControl size=3 multiple>
         <OPTION VALUE=OneValue>One</OPTION>
         <OPTION VALUE=TwoValue>Two</OPTION>
         <OPTION VALUE=ThreeValue>Three</OPTION>
this is a sample to do it(its better way I guess to help;))

Hovik Melkomian.
Old January 15th, 2005, 06:09 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

Because of the nature of select boxes options cannot be transferred between windows, you need something like the following in your child window:
function addOptionToOpener(Listbox, Text, Value)
  var oOption = self.opener.document.createElement("option");
  oOption.text = Text;
  oOption.value = Value;
and then call:
addOptionToOpener(self.opener.document.mainForm.myListBox, "New Text", "New Value");

Joe (Microsoft MVP - XML)
Old October 10th, 2006, 02:02 PM
Registered User
Join Date: Nov 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to omarps Send a message via Yahoo to omarps

a simpler approach...
encapsulate your the new option functionality on an opener function... then just call window.opener.my_new_option_funtion('select_id'); and that's all.


Similar Threads
Thread Thread Starter Forum Replies Last Post
window.opener for mozilla jun99 Javascript 3 September 1st, 2009 08:57 AM
Problem accessing the window.opener.opener maryuob Javascript 3 January 18th, 2006 05:25 AM
Populate a opener listbox from pop up window Vash Javascript How-To 1 January 15th, 2005 06:13 AM
window.opener help fs22 Javascript How-To 6 May 9th, 2004 10:50 PM
window.opener.refresh matt.fields Javascript How-To 1 November 3rd, 2003 05:13 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.