By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,688 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,688 IT Pros & Developers. It's quick & easy.

Appending option items in dropdown in parent window from dropdown in Child window in IE

P: n/a
I am trying to append options to dropdown in parent window from option
items in child window.

In parent window, I have following code:

<script language="javascript">
function AddItem2DropDown(item){
exists = false;
for(d=0;d<drpDwn.length;d++){
if(drpDwn.options[d].value == item.value)
exists = true;
}
if(exists == false)
drpDwn.options[drpDwn.length] = item;
}
</script>

Then in child window, I have following code:

<script language="javascript">
function AddItems(child){
for(i=0;i<child.length;i++){
if(child.options[i].selected == true)
window.opener.AddItem2DropDown(child.options[i]);
}
}
</script>

It works in Firefox, but not in IE6. Has anyone got this working in
IE6?

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Bostonasian wrote:
I am trying to append options to dropdown in parent window from option
items in child window.

In parent window, I have following code:

<script language="javascript">
function AddItem2DropDown(item){
exists = false;
for(d=0;d<drpDwn.length;d++){
if(drpDwn.options[d].value == item.value)
exists = true;
}
if(exists == false)
drpDwn.options[drpDwn.length] = item;
}
</script>

Then in child window, I have following code:

<script language="javascript">
function AddItems(child){
for(i=0;i<child.length;i++){
if(child.options[i].selected == true)
window.opener.AddItem2DropDown(child.options[i]);
}
}
</script>

It works in Firefox, but not in IE6. Has anyone got this working in
IE6?


Not criticising, but I'm impressed that this works in any browser.
You've succeeded in 'cutting' an Option object from one window's list
box and 'pasting' it into a list box in an entirely different window.
You don't tell us which error you get in IE (at best, I get "the server
threw an exception"), but in any case I'd recommend a different approach.

Firstly, in your parent window, if you make your 'add' function a method
of the window, its existence can be easily tested for. Then, have it
create a completely fresh Option object based upon the parameter(s)
passed to it:

window.add2DropDown = function(str, val)
{
var selBox = document.forms[0].elements["drpDwn"];
var opt = new Option;

opt.text = str;
opt.value = val;

selBox.options[selBox.length] = opt;
}

(I haven't duplicated your 'test if already exists' code).

In your child window, test for the existence: first, of the parent
window and then of the 'add' function. If successful, pass it the value
(or values) that indicate the Option you want to create:

....
if(window.opener && window.opener.add2DropDown)
{
window.opener.add2DropDown(child.options[i].text, child.options[i].value);
// then remove the item from the child's list box:
child.options[i] = null;
}...
Jul 23 '05 #2

P: n/a
Great! Thanks, your code works.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.