JavaScript bracket notation

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

    JavaScript bracket notation

    Hi,

    I have difficulty understanding how square brackets work in JavaScript
    and this is causing me difficulty retrieving the values of the options
    of my Select controls.

    My form contains many select boxes. Their names are stored in
    variables(in this case concat is the select control name)
    and I have no trouble accessing methods for the select object ie:
    document.Allan. elements[concat].selectedIndex;

    I would like to get the value of the selectedIndex but I am having
    difficulty because I don't know the right syntax. I'm looking for a
    statement like the one that follows but uses variables

    document.form.t heDay.options[2].text


    Could some one help me use the right syntax?

    Thanks,
    Larry

  • Randy Webb

    #2
    Re: JavaScript bracket notation

    larry said the following on 8/8/2005 3:22 PM:
    [color=blue]
    > Hi,
    >
    > I have difficulty understanding how square brackets work in JavaScript
    > and this is causing me difficulty retrieving the values of the options
    > of my Select controls.
    >
    > My form contains many select boxes. Their names are stored in
    > variables(in this case concat is the select control name)
    > and I have no trouble accessing methods for the select object ie:
    > document.Allan. elements[concat].selectedIndex;
    >
    > I would like to get the value of the selectedIndex but I am having
    > difficulty because I don't know the right syntax. I'm looking for a
    > statement like the one that follows but uses variables
    >
    > document.form.t heDay.options[2].text
    >
    >
    > Could some one help me use the right syntax?[/color]

    document.forms[var1Here].elements[var2Here].***

    Where *** is any of the properties of the elements objects.

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • RobG

      #3
      Re: JavaScript bracket notation

      larry wrote:[color=blue]
      > Hi,
      >
      > I have difficulty understanding how square brackets work in JavaScript
      > and this is causing me difficulty retrieving the values of the options
      > of my Select controls.
      >
      > My form contains many select boxes. Their names are stored in
      > variables(in this case concat is the select control name)
      > and I have no trouble accessing methods for the select object ie:
      > document.Allan. elements[concat].selectedIndex;[/color]

      That accesses the selectedIndex property, not a method. See below.
      [color=blue]
      >
      > I would like to get the value of the selectedIndex but I am having
      > difficulty because I don't know the right syntax. I'm looking for a
      > statement like the one that follows but uses variables
      >
      > document.form.t heDay.options[2].text
      >
      >
      > Could some one help me use the right syntax?[/color]

      Use square brackets to access properties, round brackets to use
      methods and quote string literals:

      <URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>

      Using IE it is very easy to get confused as often the two appear to be
      treated the same.


      --
      Rob

      Comment

      • Matt Kruse

        #4
        Re: JavaScript bracket notation

        larry wrote:[color=blue]
        > document.Allan. elements[concat].selectedIndex;
        > I would like to get the value of the selectedIndex but I am having
        > difficulty because I don't know the right syntax. I'm looking for a
        > statement like the one that follows but uses variables
        > document.form.t heDay.options[2].text[/color]

        The simple answer to your question is:

        var i = document.forms['Allan'].elements[concat].selectedIndex;
        alert(document. forms['Allan'].elements[concat].options[i].value);

        or,

        var el = document.forms['Allan'].elements[concat];
        alert(el.option s[el.selectedInde x].value);

        Assumptions:
        1) It is a single-select list, not multi-select
        2) There is an option selected

        --
        Matt Kruse




        Comment

        Working...