using var syntax problem

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

    using var syntax problem

    Hi All,

    Newbie here needing some syntax assistance. Here is the code:

    function fnSelectOption( fieldname)
    {
    document.mainfo rm. + fieldname + .value = "value";
    }

    It obvliously bombs where I am trying to ref the param "fieldname" . I
    realize it is a simple syntax issue but I cannot seem to figure it out. Any
    help is greatly appreicated.

    JC




    -----= 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! =-----
  • MikeB

    #2
    Re: using var syntax problem

    <script language="jscri pt">
    function fnSelectOption( fieldname)
    {
    // mainform is referenced in the forms collection and
    // fieldname is referenced by name as a form element.
    document.forms( "mainform")(fie ldname).value = "value";
    }

    </script>
    </head>

    <body>
    <form name="mainform" >
    <input type="text" name="text1" width="22">
    <input type="button" value="Click Me" onclick="fnSele ctOption('text1 ')";>


    </form>

    </body>



    "DakotaCom" <jcalhoun@river valleyit.com> wrote in message news:3fef6848_3 @corp.newsgroup s.com...[color=blue]
    > Hi All,
    >
    > Newbie here needing some syntax assistance. Here is the code:
    >
    > function fnSelectOption( fieldname)
    > {
    > document.mainfo rm. + fieldname + .value = "value";
    > }
    >
    > It obvliously bombs where I am trying to ref the param "fieldname" . I
    > realize it is a simple syntax issue but I cannot seem to figure it out. Any
    > help is greatly appreicated.
    >
    > JC
    >
    >
    >
    >
    > -----= 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! =-----[/color]


    Comment

    • Janwillem Borleffs

      #3
      Re: using var syntax problem

      MikeB wrote:[color=blue]
      > <script language="jscri pt">[/color]

      No need for jscript here, just use:
      <script type="text/javascript">
      [color=blue]
      > function fnSelectOption( fieldname)
      > {
      > // mainform is referenced in the forms collection and
      > // fieldname is referenced by name as a form element.
      > document.forms( "mainform")(fie ldname).value = "value";
      > }
      >[/color]

      This should be:
      document.forms["mainform"][fieldname].value = "value";

      (document.forms is not a function and you will get errors in other browsers
      than IE)


      JW



      Comment

      • Richard Cornford

        #4
        Re: using var syntax problem

        "MikeB" <m.byerleyATVer izonDottieNetti e> wrote in message
        news:R8WdnVfFC5 TW83KiRVn-vw@comcast.com. ..[color=blue]
        > <script language="jscri pt">
        > function fnSelectOption( fieldname)
        > {
        > // mainform is referenced in the forms collection and
        > // fieldname is referenced by name as a form element.
        > document.forms( "mainform")(fie ldname).value = "value";
        > }[/color]
        <snip>

        An obvious alternative to using IE specific code and shielding it form
        non-IE browsers is by specifying the language attribute as jscript would
        be to use the standard javascript/ECMA Script square bracket notation
        property accessor syntax. Which is also fully supported by JScript:-

        document.forms['mainform'][fieldname].value = "value";
        -or-
        document.forms['mainform'].elements[fieldname].value = "value";
        -or-
        document.forms. mainform.elemen ts[fieldname].value = "value";

        <URL: http://www.jibbering.com/faq/#FAQ4_39 >

        Richard.


        Comment

        • DakotaCom

          #5
          Re: using var syntax problem

          Thanks for the reponses! This seems to work for what I need, it also seems
          to be the cleanest:

          document.mainfo rm[fieldname].value = "value";

          JC


          "MikeB" <m.byerleyATVer izonDottieNetti e> wrote in message
          news:R8WdnVfFC5 TW83KiRVn-vw@comcast.com. ..[color=blue]
          > <script language="jscri pt">
          > function fnSelectOption( fieldname)
          > {
          > // mainform is referenced in the forms collection and
          > // fieldname is referenced by name as a form element.
          > document.forms( "mainform")(fie ldname).value = "value";
          > }
          >
          > </script>
          > </head>
          >
          > <body>
          > <form name="mainform" >
          > <input type="text" name="text1" width="22">
          > <input type="button" value="Click Me" onclick="fnSele ctOption('text1 ')";>
          >
          >
          > </form>
          >
          > </body>
          >
          >
          >
          > "DakotaCom" <jcalhoun@river valleyit.com> wrote in message[/color]
          news:3fef6848_3 @corp.newsgroup s.com...[color=blue][color=green]
          > > Hi All,
          > >
          > > Newbie here needing some syntax assistance. Here is the code:
          > >
          > > function fnSelectOption( fieldname)
          > > {
          > > document.mainfo rm. + fieldname + .value = "value";
          > > }
          > >
          > > It obvliously bombs where I am trying to ref the param "fieldname" . I
          > > realize it is a simple syntax issue but I cannot seem to figure it out.[/color][/color]
          Any[color=blue][color=green]
          > > help is greatly appreicated.
          > >
          > > JC
          > >
          > >
          > >
          > >
          > > -----= 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! =-----[/color]
          >
          >[/color]




          -----= 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

          • DakotaCom

            #6
            Re: using var syntax problem

            Thanks for the reponses! This seems to work for what I need, it also seems
            to be the cleanest:

            document.mainfo rm[fieldname].value = "value";

            JC


            "MikeB" <m.byerleyATVer izonDottieNetti e> wrote in message
            news:R8WdnVfFC5 TW83KiRVn-vw@comcast.com. ..[color=blue]
            > <script language="jscri pt">
            > function fnSelectOption( fieldname)
            > {
            > // mainform is referenced in the forms collection and
            > // fieldname is referenced by name as a form element.
            > document.forms( "mainform")(fie ldname).value = "value";
            > }
            >
            > </script>
            > </head>
            >
            > <body>
            > <form name="mainform" >
            > <input type="text" name="text1" width="22">
            > <input type="button" value="Click Me" onclick="fnSele ctOption('text1 ')";>
            >
            >
            > </form>
            >
            > </body>
            >
            >
            >
            > "DakotaCom" <jcalhoun@river valleyit.com> wrote in message[/color]
            news:3fef6848_3 @corp.newsgroup s.com...[color=blue][color=green]
            > > Hi All,
            > >
            > > Newbie here needing some syntax assistance. Here is the code:
            > >
            > > function fnSelectOption( fieldname)
            > > {
            > > document.mainfo rm. + fieldname + .value = "value";
            > > }
            > >
            > > It obvliously bombs where I am trying to ref the param "fieldname" . I
            > > realize it is a simple syntax issue but I cannot seem to figure it out.[/color][/color]
            Any[color=blue][color=green]
            > > help is greatly appreicated.
            > >
            > > JC
            > >
            > >
            > >
            > >
            > > -----= 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! =-----[/color]
            >
            >[/color]




            -----= 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

            • Lasse Reichstein Nielsen

              #7
              Re: using var syntax problem

              "DakotaCom" <jcalhoun@river valleyit.com> writes:

              Please don't top post, or at least trim your quotes.
              [color=blue]
              > Thanks for the reponses! This seems to work for what I need, it also seems
              > to be the cleanest:
              >
              > document.mainfo rm[fieldname].value = "value";[/color]

              "*Seems* to work" is correct, at least for the browser you used.
              It fails in, e.g., Mozilla/Netscape, because they don't make the form
              a property of the document element.

              Use
              document.forms['mainform'] (or the equivalent: document.forms. mainform)
              instead of just
              document.mainfo rm
              That is the minimum you can write that works in all Javascript enabled
              browsers.

              Personally I prefer to use the "elements" collection, so it is
              document.forms['mainform'].elements[fieldname].value = "value"
              I think it is most consistent.

              /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

              • MikeB

                #8
                Re: using var syntax problem

                How come you guys always answer AFTER you've had your coffee.... ;-)

                "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
                news:bsnsb9$84o $1$8300dec7@new s.demon.co.uk.. .[color=blue]
                > "MikeB" <m.byerleyATVer izonDottieNetti e> wrote in message
                > news:R8WdnVfFC5 TW83KiRVn-vw@comcast.com. ..[color=green]
                > > <script language="jscri pt">
                > > function fnSelectOption( fieldname)
                > > {
                > > // mainform is referenced in the forms collection and
                > > // fieldname is referenced by name as a form element.
                > > document.forms( "mainform")(fie ldname).value = "value";
                > > }[/color]
                > <snip>
                >
                > An obvious alternative to using IE specific code and shielding it form
                > non-IE browsers is by specifying the language attribute as jscript would
                > be to use the standard javascript/ECMA Script square bracket notation
                > property accessor syntax. Which is also fully supported by JScript:-
                >
                > document.forms['mainform'][fieldname].value = "value";
                > -or-
                > document.forms['mainform'].elements[fieldname].value = "value";
                > -or-
                > document.forms. mainform.elemen ts[fieldname].value = "value";
                >
                > <URL: http://www.jibbering.com/faq/#FAQ4_39 >
                >
                > Richard.
                >
                >[/color]


                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: using var syntax problem

                  Lasse Reichstein Nielsen wrote:
                  [color=blue]
                  > "DakotaCom" <jcalhoun@river valleyit.com> writes:[color=green]
                  >> Thanks for the reponses! This seems to work for what I need, it also seems
                  >> to be the cleanest:
                  >>
                  >> document.mainfo rm[fieldname].value = "value";[/color]
                  >
                  > "*Seems* to work" is correct, at least for the browser you used.
                  > It fails in, e.g., Mozilla/Netscape, because they don't make the form
                  > a property of the document element.[/color]

                  WFM in Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7a)
                  Gecko/2004010908.

                  But I agree to the rest of your statements because of
                  [color=blue]
                  > [example using document.forms]
                  > That is the minimum you can write that works in all Javascript enabled
                  > browsers.
                  > [...][/color]


                  PointedEars

                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: using var syntax problem

                    Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                    [color=blue]
                    > WFM in Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7a)
                    > Gecko/2004010908.[/color]

                    Mozilla makes the form a property of the document if it is named using
                    the name attribute, and not if it is named only using the id attribute.
                    In the example I used (as most of the code I write) I only used id.

                    /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

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: using var syntax problem

                      Lasse Reichstein Nielsen wrote:
                      [color=blue]
                      > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
                      >> WFM in Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7a)
                      >> Gecko/2004010908.[/color]
                      >
                      > Mozilla makes the form a property of the document if it is named using
                      > the name attribute, and not if it is named only using the id attribute.[/color]

                      True. Interestingly, it makes form elements a property of the form
                      object even if only the ID attribute is used.
                      [color=blue]
                      > In the example I used (as most of the code I write) I only used id.[/color]

                      You know, my magic Crystal Ball[tm] is still on holiday :)

                      Besides, you know that you become incompatible with older browsers
                      when using only the ID attribute for naming elememts but also access
                      them through collections/arrays?


                      PointedEars

                      Comment

                      Working...