Click to See Complete Forum and Search --> : list box selected index behavior in IE & NS4


jnmunsey
11-12-2003, 12:54 AM
Hi, I have searched for help a while and decided to ask now.

I want be able to change the selected item in a dropdown list box(not multiple) via javascript. However, I want to do it based on the value of the option, not the index number.

In IE it is simple:
document.testform.testselect.value="value of item";

In NS though, afaik you have to set the index # of the option:
document.testform.testselect.selectedIndex = item_num;

I really would like to set it based on the VALUE of the option as it works in IE.

You can view a demo of what I am doing at the link below. Both versions work in IE, but only the NS4 version works in NS4.

While these work, I don't want to use the index position of the options. With what I am working on changing via the VALUE is pretty important.

http://www.maxlang.com/test/parent_child/

Thanks

John M

Khalid Ali
11-12-2003, 06:29 AM
confused.......

you can get the selected items value in all broswers..for some reasons I missed what you are after..

Fang
11-12-2003, 08:50 AM
You need to loop through the select options to find the option value to be selected:

function change_selection(item_num) {
var f=window.opener.document.testform.testselect; //select name testselect
for (var i=0; i<f.length; i++) {
if (f.options[i].value==item_num) {
f.options[i].selected=true;
}
}
}

jnmunsey
11-12-2003, 11:13 AM
Fang nailed it.

Someone else on a different forum answered it las night. I got this and it works perfectly:

function change_selection(item_num){
var w = window.opener;
if (w && !w.closed){
var s = w.document.testform.testselect;
for (var i=0;i<s.options.length;i++){
if (s.options[i].value==item_num){
s.selectedIndex = i;
break;
}
}
}
window.close();
}
...
<a href="#" onclick="change_selection(19);return false">Lizard - Brown (+$100.00)</a>

I can also use the text value of the option as well if I want.

Thanks for your help.