471,086 Members | 899 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

accessing select object via value

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
Jul 20 '05 #1
2 1915
Steven Post wrote:
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

Jul 20 '05 #2
In article <3F**************@yahoo.com>,
Brian Genisio <Br**********@yahoo.com> wrote:
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
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by rosemm | last post: by
1 post views Thread by Terry A. Haimann | last post: by
6 posts views Thread by Chris Styles | last post: by
7 posts views Thread by Chuck Anderson | last post: by
6 posts views Thread by Chuck Anderson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.