Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set selectedIndex after populating select box

Status
Not open for further replies.

cyclopsvs

Programmer
Feb 21, 2007
7
0
0
NL
Hello Javascript Users,

I recently started doing some stuff in javascript again but now i am running into some problems which i can't find a solution to.

I have two select boxes in a form the values of the second select box are populated with specific values based on the selection made in the first select box ( this completely works ).

function UpdateMethodField( sel ){
var secondsel = document.getElementById('id_method');
var selvalue = sel.options[sel.selectedIndex].value;
var count = secondOptions[ selvalue ].length;
secondsel.options.length = 0;

for( i =0; i<count; i++)
secondsel.options = new Option( secondOptions[selvalue][0],secondOptions[selvalue][1] );
}

secondOptions is the array holding the values to each specific value in the first select box.

The problem is when i populate the second select box i also want to give along the selected="selected" value. i thought i could do this with the following code.

secondsel.selectedIndex="199";

But this won't work. however it gives some reaction because the select box will give a complete empty field with no index. I checked if this value was in the array and it is so that is not the problem.

Does anyone know how to solve this.

any help would be greatly appreciated,
thank in advance.

Richard
 
[tt].selectedIndex[/tt] can be a bit flaky depending on browser as to whether it's implemented as a readonly or read/write property.

You're better off setting the [tt].selected[/tt] property of the desired option to [tt]true[/tt].

Code:
var intIndexOfOption = 99; //Or however you wish to specify it
secondsel.options[intIndexOfOption].selected = true;

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
.selectedIndex can be a bit flaky depending on browser as to whether it's implemented as a readonly or read/write property.

dwarfthrower, which browsers don't allow it as a read/write property? I've never experienced that before.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
I'm probably harking back a decade or so, NN4 had issues with it IIRC.

I'm fairly sure that all modern browsers support it as read/write.... but it's one of those "voodoo programming" things that has stuck with me over the years. Irrational, but I still do it anyway.

Like baby chimpanzees... depending on whether or not a young chimpanzee encounters a snake, or an electrical cable first in it's life, it will develop a lifelong aversion to either snakes or electrical cables, but be perfectly fine with the one it didn't see first. So too the minds of young programmers imprint on whichever method of setting selected options in select boxes first bit them on the nether regions.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
Thanks for the info dwarfthrower, I was never aware of inconsistencies with NN4. Truth be told, I didn't really get into web programming until after most people had stopped caring about Netscape. [smile]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
thanks for the help,

although it seems i made a mistake about what i actually needed to get the correct value in there.

the variable i have are is the value of the options that needs to be selected. Is there a way to go from that value to the index of the options that corresponds in a easy way ? ( i could write a loop to find the similar value )

Or is there a better way to approach this problem,

thanks in advance,

Richard
 
Sorry for that post,

didn't really think of a way to solve that.
I was already looping threw the list so it was easy to see if a value from the array matched the known value.

based on where i was in the loop, the index was easily set.

thanks again for you help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top