Keep getting undefined variable error

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

    Keep getting undefined variable error

    Hi!

    Any ideas as to how I can get ride of the script error undefined
    variable "Exp_Month" ?

    I am trying to get this drop down list to default to the current month
    as opposed to 01.

    Thank you in advance.

    <SELECT size="1" name="Exp_Month ">
    <OPTION value="01" selected>1</OPTION>
    <OPTION value="02">2</OPTION>
    <OPTION value="03">3</OPTION>
    <OPTION value="04">4</OPTION>
    <OPTION value="05">5</OPTION>
    <OPTION value="06">6</OPTION>
    <OPTION value="07">7</OPTION>
    <OPTION value="08">8</OPTION>
    <OPTION value="09">9</OPTION>
    <OPTION value="10">10</OPTION>
    <OPTION value="11">11</OPTION>
    <OPTION value="12">12</OPTION>
    </SELECT>

    <script type="text/javascript">
    var now = new Date()
    Exp_Month.selec tedIndex = now.getMonth()
    </script>
  • LJL

    #2
    Re: Keep getting undefined variable error

    securitypro@cfl .rr.com (Chuck) wrote in
    news:85d32cab.0 402081731.42763 230@posting.goo gle.com:
    [color=blue]
    > <SELECT size="1" name="Exp_Month ">
    > <OPTION value="01" selected>1</OPTION>
    > <OPTION value="02">2</OPTION>
    > <OPTION value="03">3</OPTION>
    > <OPTION value="04">4</OPTION>
    > <OPTION value="05">5</OPTION>
    > <OPTION value="06">6</OPTION>
    > <OPTION value="07">7</OPTION>
    > <OPTION value="08">8</OPTION>
    > <OPTION value="09">9</OPTION>
    > <OPTION value="10">10</OPTION>
    > <OPTION value="11">11</OPTION>
    > <OPTION value="12">12</OPTION>
    > </SELECT>
    >
    > <script type="text/javascript">
    > var now = new Date()
    > Exp_Month.selec tedIndex = now.getMonth()
    > </script>[/color]



    The easiest way I have found, is to change the script to:

    <script type="text/javascript">
    var now = new Date();
    document.getEle mentById('Exp_M onth').selected Index = now.getMonth();
    </script>


    Alternatively, name the form. If named "form_name" , call the element by:

    document.form_n ame.Exp_Month. selectedIndex = now.getMonth();

    One last thing, which you probably know . . .
    Be aware that months are zero-based, January is zero, Februaryis one,
    etc. So calling the element's selectedIndex works, because the options are
    zero-based liked the months. However, your values are 1-12. If you plan
    to use the element's values, you may want to renumber them 0-11.
    It only makes a difference if you are going to use the value
    information for some month-related/date related script function in the
    future.

    Good luck,
    LJL

    Comment

    • Michael Winter

      #3
      Re: Keep getting undefined variable error

      On Mon, 09 Feb 2004 04:51:46 GMT, LJL <none@nowhere.c om> wrote:

      [snip]
      [color=blue]
      > The easiest way I have found, is to change the script to:
      >
      > <script type="text/javascript">
      > var now = new Date();
      > document.getEle mentById('Exp_M onth').selected Index = now.getMonth();[/color]

      I beg your pardon? You cannot use document.getEle mentById with a named
      element. Yes, it works in IE, but it won't work in all the other browsers.
      document.getEle mentById should be used with ID'd elements. If you want to
      get a named element using the DOM, use document.getEle mentsByName.

      If you are trying to access a named element within a form, use[1]:

      document.formNa me.Exp_Month.se lectedIndex

      If you don't use a form, ID the select element.

      Be aware that the DOM is not supported well (or at all) by all browsers.
      You should use feature detection to determine support for a particular
      method. The example below assumes you ID'd the element, Exp_Month.

      var expMonth = null;

      if( document.getEle mentById ) {
      expMonth = document.getEle mentById( 'Exp_Month' );
      } else if( document.all ) {
      expMonth = document.all[ 'Exp_Month' ];
      }

      expMonth.select edIndex = .....

      [snip]

      Mike


      [1] You can also use a collection-oriented accessor method. It is slightly
      slower and more to type, but can be used to access ID'd elements, and
      elements with names that include characters that are interpreted as
      operators in JavaScript (+, -, [, ], *, etc). The same expression would be
      written as:

      document.forms['formName'].elements['Exp_Month'].selectedIndex

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

      Comment

      • Michael Winter

        #4
        Re: Keep getting undefined variable error

        On Mon, 09 Feb 2004 08:44:58 GMT, Michael Winter
        <M.Winter@bluey onder.co.invali d> wrote:

        [snip]
        [color=blue]
        > var expMonth = null;
        >
        > if( document.getEle mentById ) {
        > expMonth = document.getEle mentById( 'Exp_Month' );
        > } else if( document.all ) {
        > expMonth = document.all[ 'Exp_Month' ];
        > }
        >
        > expMonth.select edIndex = ...[/color]

        This example should end

        if( expMonth ) {
        expMonth.select edIndex = ...
        }

        [snip]

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Bas Cost Budde

          #5
          Re: Keep getting undefined variable error

          Michael Winter wrote:
          [color=blue]
          > On Mon, 09 Feb 2004 04:51:46 GMT, LJL <none@nowhere.c om> wrote:
          >
          > [snip]
          >[color=green]
          >> The easiest way I have found, is to change the script to:
          >>
          >> <script type="text/javascript">
          >> var now = new Date();
          >> document.getEle mentById('Exp_M onth').selected Index = now.getMonth();[/color]
          >
          >
          > I beg your pardon? You cannot use document.getEle mentById with a named
          > element. Yes, it works in IE, but it won't work in all the other
          > browsers. document.getEle mentById should be used with ID'd elements. If
          > you want to get a named element using the DOM, use
          > document.getEle mentsByName.[/color]

          Well, since the name attribute is deprecated anyway, it is probably best
          to switch to the id attribute. Or, include both, with the same value.

          Recently I read that IE indeed allows element reference by id (no, that
          should be: name) immediately, but that makes for hard readable code
          unless you are well versed in the document structure. Therefore I think
          document.getEle mentById is the better way. I wish they'd chosen a
          shorter spelling...
          [color=blue]
          > Be aware that the DOM is not supported well (or at all) by all browsers.
          > You should use feature detection to determine support for a particular
          > method.[/color]

          I agree.

          --
          Bas Cost Budde

          but the domain is nl

          Comment

          • Richard Cornford

            #6
            Re: Keep getting undefined variable error

            "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
            news:opr23hacfx 5vklcq@news-text.blueyonder .co.uk...
            <snip>[color=blue]
            > document.formNa me.Exp_Month.se lectedIndex[/color]
            <snip>[color=blue]
            >[1] You can also use a collection-oriented accessor method. It is
            >slightly slower and more to type, but can be used to access ID'd
            >elements, and elements with names that include characters that
            >are interpreted as operators in JavaScript (+, -, [, ], *, etc).
            >The same expression would be written as:
            >
            > document.forms['formName'].elements['Exp_Month'].selectedIndex[/color]

            It is not the use of the collections that allows the "illegal"
            characters to be used in javascript property accessors it is the square
            bracket notation that makes it possible. The property names of an (any)
            object are unrestricted by ECMA 262 (probably there are implementation
            imposed limits, such as total property name length). It is the dot
            notation accessors that impose a restriction on the names used because
            the items between the dots must conform to the production rules for an
            identifier.

            As the two forms of property accessor syntax are equivalent (in terms of
            how they work) it is possible to use square bracket notation wherever
            dot notation is used, so the above shortcut form accessor could be any
            of (and more):-

            document["formName"].Exp_Month.sele ctedIndex
            document.formNa me["Exp_Month"].selectedIndex
            document["formName"]["Exp_Month"].selectedIndex
            document["formName"]["Exp_Month"]["selectedIn dex"]
            window["document"]["formName"]["Exp_Month"]["selectedIn dex"]

            - and the strings used in the bracket notation can hold any character
            sequence.

            Similarly the collections based assessors could use dot notation
            (assuming the names/IDs conformed with the identifier rules):-

            document.forms. formName.elemen ts.Exp_Month.se lectedIndex

            The reason that I use the longer collections based accessors with the
            form and control names in bracket notation is for the clarity it
            provides in the source code. The collections access leaves a reader of
            the code in no doubt that the subject of the accessor is a control
            within a form (as they can clearly read "forms" and "elements" in the
            accessor). And the use of bracket notation in that context separates the
            HTML namespace from the javascript namespace.

            Speed of resolution of the longer accessors is not usually an issue as I
            would assign a reference to the - elements - collection to a local
            variable anyway:-

            var els = document.forms['formName'].elements;

            - and then refer to the controls relative to that, so most of the work
            resolving the form accessors is only done once. (it also makes the
            amount of extra typing involved when using the longer form
            insignificant)

            Richard.


            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: Keep getting undefined variable error

              Bas Cost Budde <bas@heuveltop. org> writes:
              [color=blue]
              > Well, since the name attribute is deprecated anyway, it is probably
              > best to switch to the id attribute.[/color]

              It is not deprecated on form controls. The example that is currently
              discussed is for a select element, i.e., a form control (if inside
              a form, at least).
              [color=blue]
              > Or, include both, with the same value.[/color]

              When used on form controls, name and id values doesn't have to be
              identical. The id value gives the anchor name of the object, the name
              value gives the control name (the one that is used when the form is
              submitted).

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Michael Winter

                #8
                Re: Keep getting undefined variable error

                On Mon, 9 Feb 2004 17:46:40 -0000, Richard Cornford
                <Richard@litote s.demon.co.uk> wrote:
                [color=blue]
                > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                > news:opr23hacfx 5vklcq@news-text.blueyonder .co.uk...
                > <snip>[color=green]
                >> document.formNa me.Exp_Month.se lectedIndex[/color]
                > <snip>[color=green]
                >> [1] You can also use a collection-oriented accessor method. It is
                >> slightly slower and more to type, but can be used to access ID'd
                >> elements, and elements with names that include characters that
                >> are interpreted as operators in JavaScript (+, -, [, ], *, etc).
                >> The same expression would be written as:
                >>
                >> document.forms['formName'].elements['Exp_Month'].selectedIndex[/color]
                >
                > It is not the use of the collections that allows the "illegal"
                > characters to be used in javascript property accessors it is the square
                > bracket notation that makes it possible.[/color]

                I didn't say that, nor did I (intend to) imply it.

                [snip]
                [color=blue]
                > document["formName"].Exp_Month.sele ctedIndex
                > document.formNa me["Exp_Month"].selectedIndex
                > document["formName"]["Exp_Month"].selectedIndex
                > document["formName"]["Exp_Month"]["selectedIn dex"]
                > window["document"]["formName"]["Exp_Month"]["selectedIn dex"]
                >
                > - and the strings used in the bracket notation can hold any character
                > sequence.
                >
                > Similarly the collections based assessors could use dot notation
                > (assuming the names/IDs conformed with the identifier rules):-
                >
                > document.forms. formName.elemen ts.Exp_Month.se lectedIndex[/color]

                I've seen collections accessed like that, but it's usually been in badly
                written, IE-only scripts so I've always ignored it. I never realised that
                the bracket- and dot-notations were quite so interchangable.

                Mike

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                Comment

                • LJL

                  #9
                  Re: Keep getting undefined variable error

                  Michael Winter <M.Winter@bluey onder.co.invali d> wrote in
                  news:opr23hacfx 5vklcq@news-text.blueyonder .co.uk:
                  [color=blue]
                  > I beg your pardon? You cannot use document.getEle mentById with a named
                  > element. Yes, it works in IE, but it won't work in all the other
                  > browsers. document.getEle mentById should be used with ID'd elements.
                  > If you want to get a named element using the DOM, use
                  > document.getEle mentsByName.
                  >
                  > [snip]
                  >
                  > Mike[/color]


                  You're absolutely right. My apologies. I had added the ID tag to my test
                  bed when I tried the mod to make sure it worked. I forgot completely about
                  having done so when I sent my reply to the original post.

                  Thanks for pointing out my shortfall.

                  LJL

                  Comment

                  • Dennis M. Marks

                    #10
                    Re: Keep getting undefined variable error

                    I have read the following message from securitypro@cfl .rr.com (Chuck)
                    and have decided to lend my vast knowledge.

                    The writer said:[color=blue]
                    > Hi!
                    >
                    > Any ideas as to how I can get ride of the script error undefined
                    > variable "Exp_Month" ?
                    >
                    > I am trying to get this drop down list to default to the current month
                    > as opposed to 01.
                    >
                    > Thank you in advance.
                    >
                    > <SELECT size="1" name="Exp_Month ">
                    > <OPTION value="01" selected>1</OPTION>
                    > <OPTION value="02">2</OPTION>
                    > <OPTION value="03">3</OPTION>
                    > <OPTION value="04">4</OPTION>
                    > <OPTION value="05">5</OPTION>
                    > <OPTION value="06">6</OPTION>
                    > <OPTION value="07">7</OPTION>
                    > <OPTION value="08">8</OPTION>
                    > <OPTION value="09">9</OPTION>
                    > <OPTION value="10">10</OPTION>
                    > <OPTION value="11">11</OPTION>
                    > <OPTION value="12">12</OPTION>
                    > </SELECT>
                    >
                    > <script type="text/javascript">
                    > var now = new Date()
                    > Exp_Month.selec tedIndex = now.getMonth()
                    > </script>
                    >[/color]

                    and my reply is:
                    I'm not sure but try putting the select within a form.

                    --
                    Dennis M. Marks

                    Replace domain.invalid with dcsi.net


                    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                    Comment

                    • Bas Cost Budde

                      #11
                      Re: Keep getting undefined variable error

                      Lasse Reichstein Nielsen wrote:
                      [color=blue]
                      > Bas Cost Budde <bas@heuveltop. org> writes:
                      >[color=green]
                      >>Well, since the name attribute is deprecated anyway, it is probably
                      >>best to switch to the id attribute.[/color]
                      >
                      > It is not deprecated on form controls.[/color]

                      You're right.
                      [color=blue][color=green]
                      >>Or, include both, with the same value.[/color]
                      >
                      > When used on form controls, name and id values doesn't have to be
                      > identical. The id value gives the anchor name of the object, the name
                      > value gives the control name (the one that is used when the form is
                      > submitted).[/color]

                      Thanks. I had trouble using getElementById in IE since that also
                      returned matches on the name attribute, but I probably gave up on the
                      whole name attribute too early.
                      --
                      Bas Cost Budde

                      but the domain is nl

                      Comment

                      Working...