PDA

View Full Version : dynamically creating select options


tater
12-05-2003, 10:09 PM
Can you create a new option in a previous window?

I tried this and it worked

var newCompany = new Option("work", "please", false, true);

document.newPoForm.company.options[document.newPoForm.company.options.length] = newCompany;


From a window opened by the one above, this didn't any ideas why?

var newCompany = new Option("work", "please", false, true);

opener.newPoForm.company.options[opener.newPoForm.company.options.length] = newCompany;

liorean
12-05-2003, 10:15 PM
Because they are part of different documents. The DOM objects gets strange that way, and Option is probably just a shortcut for creating an HTMLOptionElement using the DOM nowadays. The DOM requires the elements to be added to the document to be created by the same document. Making the other document create the Option and then add that to the list should be pretty easy to do, however. You could create a function that does it from the first window and inject that function into the second.


(Actually there are ways to import a node from another document, but let's go the more reliable way instead.)

tater
12-05-2003, 10:21 PM
i don't understan "inject that function into the other document"

Thanks for the reply:)

liorean
12-05-2003, 10:27 PM
opener.window.injectedFunction=function(){
/*your code*/
}

opener.window.injectedFunction();Simply append it to the window object of the window you are targetting.

tater
12-05-2003, 10:38 PM
sorry, i still dont understand

where would the code you wrote go?

liorean
12-05-2003, 11:01 PM
In the window from which you want to add the option to the other window.

tater
12-05-2003, 11:08 PM
i got it to work by creating the addOption(sValue) function in the first window and then invoking that function using opener.addOption(sValue); from the second

Thanks for your help