accessing select object via value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Post

    #1

    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
  • Brian Genisio

    #2
    Re: accessing select object via value

    Steven Post wrote:
    [color=blue]
    > 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[/color]

    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

    Comment

    • Steven Post

      #3
      Re: accessing select object via value

      In article <3FFAAEC1.40909 09@yahoo.com>,
      Brian Genisio <BrianGenisio@y ahoo.com> wrote:
      [color=blue]
      > 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.[/color]

      I'll give that a shot. Thanks.

      sp

      Comment

      Working...