accessing select object via value

S

Steven Post

Hi all,

I've got a situation where I have a given string and I need to find
which option in a select object has a value matching this string. I know
I could do it by iterating through all of the options and comparing my
string to the value of each, but what I am wondering is...

Is there anyway to access the values of options in a select object as
though they were keys in an associative array, something like:

myForm.mySelect.myOption['such-and-such'].index -- returns the index of
such-and such

I know there are a zillion reasons why this particular example doesn't
work, but is there any way to do this without iterating through the
array?

Many thanks,
sp
 
B

Brian Genisio

Steven said:
Hi all,

I've got a situation where I have a given string and I need to find
which option in a select object has a value matching this string. I know
I could do it by iterating through all of the options and comparing my
string to the value of each, but what I am wondering is...

Is there anyway to access the values of options in a select object as
though they were keys in an associative array, something like:

myForm.mySelect.myOption['such-and-such'].index -- returns the index of
such-and such

I know there are a zillion reasons why this particular example doesn't
work, but is there any way to do this without iterating through the
array?

Many thanks,
sp

If you set the ID (or possibly the NAME attr, but I would stick to ID)
attribute in the option to be the same as the value, then you can do
what you are talking about. If not, you will need to itterate.

Also, if you do what I said, and the string does _not_ exist, you will
likely get a failure, so you will need to do error checking, unless you
know the string will _always_ be one of the options. Your best bet
might be to itterate through the options. As long as you dont have many
options, this will not take much time.

B

Brian
 
S

Steven Post

Brian Genisio said:
If you set the ID (or possibly the NAME attr, but I would stick to ID)
attribute in the option to be the same as the value, then you can do
what you are talking about. If not, you will need to itterate.

Also, if you do what I said, and the string does _not_ exist, you will
likely get a failure, so you will need to do error checking, unless you
know the string will _always_ be one of the options. Your best bet
might be to itterate through the options. As long as you dont have many
options, this will not take much time.

I'll give that a shot. Thanks.

sp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top