473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Frustrating select box problem

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?
Jul 23 '05 #1
6 2105
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[i].value == "test") {
oMySelect.selectedIndex = i;
break;
}
}
}

ciao, dhgm
Jul 23 '05 #2
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?
Jul 23 '05 #3
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.

Jul 23 '05 #4
John Burns wrote:
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[i];
if (oCurrentOption.value == sValue
&& oCurrentOption.text == sText
) {
oMySelect.selectedIndex = i;
break;
}
}
if (i == nOptions) {
oOptions[i] = 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
Jul 23 '05 #5
Dietmar Meier wrote:
if (i == nOptions) {
oOptions[i] = 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.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #6
Matt Kruse wrote:
if (i == nOptions) {
oOptions[i] = 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[i];
if (oCurrentOption.value == sValue
&& oCurrentOption.text == sText
) {
oSelect.selectedIndex = i;
break;
}
}
if (i == nOptions) {
try {
oOptions[i] = new Option(sText, sValue);
oSelect.selectedIndex = i;
}
catch(e) {
try {
oOptions[i] = 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
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ColinWard | last post by:
Hi. I use two different pieces of code to manipulate a recordsource for a form. The first one sets the recordsource to null when the form loads. The second is supposed to display the corresponding...
1
by: John | last post by:
Hi I have a very frustrating problem. I had a perfectly working app with a sub form on a main form. I added a few fields in tables here and there and now I can not enter data on in the sub form...
0
by: Rob Levine | last post by:
(This post also available at http://roblevine.blogspot.com/2004/11/frustrating-http-connection-behaviour.html in a slightly more readable format!) Hi All, I seem to be having a bit of a...
3
by: Loui Mercieca | last post by:
Hi, I have a very strange and frustrating error. I developed an application, tested it and it works fine. Then i deploy it to another machine ( no installation, direct file transfer ) but it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.