Frustrating select box problem

J

John Burns

Hi,
I'm trying to get javascript to change the displayed value in a combo box
(one of the <select type things) from a child window. I can get it to
control check boxes and text boxes but the select box is causing me real
problems.

Form name is newcert, selectbox name is loadcity

window.opener.document.newcert.loadcity.value = 'test';

When run, this changes the displayed option to blank. Is .value not the
option to change the displayed test in this select box???

Any ideas?
 
D

Dietmar Meier

John Burns wrote:

[Select]
window.opener.document.newcert.loadcity.value = 'test';

You need to change the selectedIndex property or one option's
selected property. If you only know the value of the option you
want to select, but not it's index, you have to iterate the
options like an array. Quickhack:

var oMyForm, oMySelect, aMyOptions, i;
if (opener
&& !opener.closed
&& (oMyform = opener.document.forms['newcert'])
&& (oMySelect = oMyForm.elements['loadcity'])
&& (aMyOptions = oMySelect.options)
) {
for (i=0; i<aMyOptions.length; i++) {
if (aMyOptions.value == "test") {
oMySelect.selectedIndex = i;
break;
}
}
}

ciao, dhgm
 
J

J. Baute

instead of setting the value of the current item, you need to select
the required already existing item in the combo box instead,

this is done by setting the options "selected" property to true, which
will then automatically select this option (duh!) :)

so if you know the index of the item to select, you can do this:

window.opener.document.newcert.loadcity.options[itemIndex].selected =
true;

otherwise you'll have to loop through the options collection, and set
the selected property to true if you find a matching value,

hope this helps,
J.
 
J

John Burns

Thanks so far.
The item I was trying to get in the list, wasn't in the list at the time, it
was a new item.
I guess this makes it a little more difficult?
 
D

Dietmar Meier

John said:
The item I was trying to get in the list, wasn't in the list at the
time, it was a new item.
I guess this makes it a little more difficult?

Not really. You can simply add an option to the options collection
(like adding an item to an array) if it doesn't exist. Another
quickhack:

function selectOrAddOption(oSelect, sText, sValue) {
var oOptions, nOptions, oCurrentOption, i;
if (oSelect
&& (oOptions = oSelect.options)
) {
nOptions = oOptions.length;
for (i=0; i<nOptions; i++) {
oCurrentOption = aMyOptions;
if (oCurrentOption.value == sValue
&& oCurrentOption.text == sText
) {
oMySelect.selectedIndex = i;
break;
}
}
if (i == nOptions) {
oOptions = new Option(sText, sValue);
oMySelect.selectedIndex = i;
}
}
}

var oMyForm, oMySelect, aMyOptions, i;
if (opener
&& !opener.closed
&& (oMyform = opener.document.forms['newcert'])
&& (oMySelect = oMyForm.elements['loadcity'])
) {
selectOrAddOption(oMySelect, "test", "42");
}

ciao, dhgm
 
M

Matt Kruse

Dietmar said:
if (i == nOptions) {
oOptions = new Option(sText, sValue);
oMySelect.selectedIndex = i;


This will cause an error in IE.
You cannot create new options and append them to a select list in any other
'window' object but your own.

So, you need to write a function in the opener window which will accept
parameters and add the new option for you. Then, call that function from the
popup window with the text and value arguments.
 
D

Dietmar Meier

Matt said:
if (i == nOptions) {
oOptions = new Option(sText, sValue);
oMySelect.selectedIndex = i;

This will cause an error in IE.
You cannot create new options and append them to a select list in any
other 'window' object but your own.

There's a trick that makes it work in IE too:

function selectOrAddOption(oWindow, oSelect, sText, sValue) {
var oOptions, nOptions, oCurrentOption, i;
if (oSelect
&& (oOptions = oSelect.options)
) {
nOptions = oOptions.length;
for (i=0; i<nOptions; i++) {
oCurrentOption = oOptions;
if (oCurrentOption.value == sValue
&& oCurrentOption.text == sText
) {
oSelect.selectedIndex = i;
break;
}
}
if (i == nOptions) {
try {
oOptions = new Option(sText, sValue);
oSelect.selectedIndex = i;
}
catch(e) {
try {
oOptions = oWindow.Option(sText, sValue);
oSelect.selectedIndex = i;
}
catch(e) {
}
}
}
}
}
function testit() {
var oMyForm, oMySelect;
if (opener
&& !opener.closed
&& (oMyForm = opener.document.forms['newcert'])
&& (oMySelect = oMyForm.elements['loadcity'])
) {
selectOrAddOption(opener, oMySelect, "test", "42");
}
}

ciao, dhgm
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top