Forum Moderators: open
bit of a long name for a post.. was trying to be descriptive :-s
anyway
i have a function that fills some dropdowns.. (for a change) and in NS6.1 (only it seems) however hard i try to fill and default a selected item , it sets itself to the first in the list regardless.
a little snippet - this one does the filling
function fillselect(field,itemarray,fieldprompt,defitem){
var i,j;
clearselect(field);
if (itemarray == null){return;}
if (fieldprompt == true){
if (field.name.indexOf('Day')!= -1){
field.options[0] = new Option('ddd');
field.options[0].value = 1;
}else{
field.options[0] = new Option('Please Choose');
field.options[0].value = null;
}
j = 1;
} else {
j = 0;
}
if (itemarray!= null) {
for (i=0; i<itemarray.length; i++) {
field.options[j] = new Option(itemarray[i][1]);
if (itemarray[i][1]!= null) {
field.options[j].value = itemarray[i][0];
}
j++;
}
if(defitem!= '' ){
field.options[defitem].selected = true;
}else{
field.options[0].selected = true;
}
}
}
if (field.name.indexOf('Day') == -1 && field.name.indexOf('Month') == -1){
for(i=0;i<years.length;i++){
yearItems[i] = [years[i],years[i]]
if(years[i] == year){defitem = i;}
}
clearselect(selYear);
fillselect(selYear,yearItems,0,defitem);
selYear.options[defitem].selected = true;
}
in this particular instance,
itemarray = [yyyy,yyyy], [2003,2003] , [2004,2004]
defitem is set to 1, so it should default to 2003
but with alerts to slow it down and things, i can see it setting it, then setting it straight back to yyyy again..
arrrgggg!
html is simply
<select name=blah onchange="somefucntionthatcallstheonesabove()">
...
cheers again .. i hope :)
nat
Not sure why you are using
itemarray = [yyyy,yyyy], [2003,2003] , [2004,2004]
Your probably right, I just haven't come across it.
I would have thought the text and the value of the option would always be the same?
I would have thought
itemarray = new Array("yyyy", 2003, ...)
would be better.
Could it be something to do with using the variable i in your fillselect function and the code that calls it? Shouldn't think so, but I would always intialise variables to something before using them.
No quick fix solution to your problem. Maybe you could express the selectedness in a different manner?
field.selectedIndex=defitem;