Select Menu Option <create on the fly>

J

jojowebdev

Picking up from Friday..
I am still having trouble creating the Select Menu option. I got parts
of the correct syntax but it is still not creating the selected carrier
as an option in the parent page.

Help apprecitate,. I currently have it commented out just to show the
line to you better:

function onCarrierSelect() {
var frm = document.carrRequestForm.carrierList.selectedIndex;
var selectCar =
document.carrRequestForm.carrierList.options[frm].text;
var varEl = "<%=varElementName%>";
if (window.opener && !window.opener.close)
var oOption = window.opener.document.createElement("OPTION");
// window.opener.form.[varEl].options.add(oOption);
oOption.innerText =selectCar;
oOption.value =selectCar;
window.close();
}
 
J

jojowebdev

It is breaking/erroring at:
var oOption = window.opener.document.createElement("OPTION");

This is a pop-up window select menu that when a value is chosen will
populate the parent window select menu.

Does oOption need to reference the form[0] and the varEl?
How does it know what form to create an option for?

I see the 4th line trys to do this:

window.opener.form[0].elements[varEl].options[window.opener.form[0].elements[varEl].options.length]
= oOption;

The error I am getting is:
Error: 'undefined' is null or not an object.

function onCarrierSelect() {
var frm = document.carrRequestForm.carrierList.selectedIndex;
var selectCar =
document.carrRequestForm.carrierList.options[frm].text;
var varEl = "<%=varElementName%>";
if (window.opener && !window.opener.close)
var oOption = window.opener.document.createElement("OPTION");
// assigns object name to create Option tag
oOption.innerText =selectCar;
oOption.value =selectCar;

window.opener.form[0].elements[varEl].options[window.opener.form[0].elements[varEl].options.length]
= oOption;
window.close();
}
 
M

Matt Kruse

It is breaking/erroring at:
var oOption = window.opener.document.createElement("OPTION");

If you're trying to do this from IE, you can't.
Creating a new option and adding it to a select list must be done from code
within the same page.
So, in your main document write a function which accepts the new Option()
parameters and creates it there.
Then in your popup, call that function in window.opener to do the work.
 
J

jojowebdev

I have the code to do it. But it is two functions I hacked together.

Does anyone know if I can "clean" it up some?

THank you.

function onCarrierSelect() {
var frm = document.carrRequestForm.carrierList.selectedIndex;
var selectCar =
document.carrRequestForm.carrierList.options[frm].text;
var varEl = "<%=varElementName%>";
if (window.opener && !window.opener.close)
var oOption = window.opener.document.createElement('option'); //
assigns object name to create Option tag
insertOptionBefore(varEl, selectCar)
window.close();
}
function insertOptionBefore(varElementName, selectCar)
{
var elSel = window.opener.document.getElementById(varElementName);
if (elSel.selectedIndex >= 0) {
var elOptNew = window.opener.document.createElement('option');
elOptNew.text = selectCar;
elOptNew.value = selectCar;
var elOptOld = elSel.options[0];
try {
elSel.add(elOptNew, elOptOld); // standards compliant; doesn't
work in IE
}
catch(ex) {
elSel.add(elOptNew, elSel.selectedIndex); // IE only
elSel.selectedIndex = elSel.selectedIndex -1
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top