[Javascript] Controling a SELECT from a child window

Nick Fitzsimons nick at nickfitz.co.uk
Wed Sep 21 02:51:49 CDT 2005


> Actually - everything works now except ADDING an option to the select
> box (on the parent page)...the following fails:

<SNIP />

>
> Perhaps you cannot add an item to a select box on a parent window?
>

As you surmise, it's probably something to do with the fact that you're
creating the option element in one window and trying to add it to the
document in another - this is not allowed under the DOM standard.

Try:

var newOptionText = window.opener.document.createTextNode("Your text");
var newOption = window.opener.document.createElement("option");
newOption.appendChild(newOptionText);
newOption.setAttribute("value", Your value");
window.opener.document.form1.mylist.appendChild(newOption);

which uses the opener window's document to create the text node, and
should therefore be OK. It's also going to work cross-browser (IE, Mozilla
Firefox, Opera and any other browser that supports the DOM), whereas I
think I'm correct in saying that new Option() is a non-standard
Internet-Explorer-only technique.

(BTW, I haven't tested the above code, but barring typing mistakes, it
should work :-)

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/




More information about the Javascript mailing list