Forum Moderators: open

Message Too Old, No Replies

NS6.1 refusing to allow select defaulting

         

natty

5:04 pm on Apr 29, 2003 (gmt 0)

10+ Year Member



hi all,

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;
}
}
}


and this one calls it with some stuff


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

HocusPocus

8:59 pm on Apr 29, 2003 (gmt 0)

10+ Year Member



Had only a quick look at your code, sort of got the jist.
A few comments-

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;

natty

9:55 am on Apr 30, 2003 (gmt 0)

10+ Year Member



cheers for the reply dude,

the function i use for filling the selects is used across the board of myt selects, which dont always have the same values and texts..

like i may have said this function works a treat in every browser except NN6.1. must i really rewrite it just for that?!?!?!
who knows..

natty

10:28 am on Apr 30, 2003 (gmt 0)

10+ Year Member



ok, in case anyone is interested. i did as suggested and chenged to
field.selectedIndex = defitem;
from
field.options[defitem].selected = true

and it sorted it.
strange.
cheers again
n