eval() not working

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

    #1

    eval() not working

    Hi

    I have a form called 'store' with many fields, and I can update the 'name'
    field ok like this

    document.store. name.value = n; //this works fine

    but I want a function to update any field, so would like to do something
    like this:

    function setStoreValue(f , v) {
    eval ("document.stor e." + f + ".value") = v;
    }

    but when I do this, nothing happens - no errors, nothing.

    Even this doesn't work:

    function setStoreNameVal ue(n) {
    eval("document. store.name.valu e = " + n);
    }


    This eval function has me beat. Is this the only way to set this up?

    Can anyone tell me how I'm doing this wrong?

    Many thanks.




  • Martin Honnen

    #2
    Re: eval() not working



    Bill wrote:
    [color=blue]
    > Hi
    >
    > I have a form called 'store' with many fields, and I can update the 'name'
    > field ok like this
    >
    > document.store. name.value = n; //this works fine
    >
    > but I want a function to update any field, so would like to do something
    > like this:
    >
    > function setStoreValue(f , v) {
    > eval ("document.stor e." + f + ".value") = v;
    > }[/color]

    Forget about eval, JavaScript 1.x allows you to use any expression to
    access the property of an object if you use square brackets:
    document.store[f].value = v;

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: eval() not working

      Bill wrote:
      [color=blue]
      > [...]
      > but I want a function to update any field, so would like to do something
      > like this:
      >
      > function setStoreValue(f , v) {
      > eval ("document.stor e." + f + ".value") = v;
      > }
      >
      > but when I do this, nothing happens - no errors, nothing.[/color]

      'cause that's fantasy syntax.

      Try this:

      function setStoreValue(f , v)
      {
      document.forms['store'].elements[f].value = v;
      }
      [color=blue]
      > This eval function has me beat. Is this the only way to set this up?[/color]

      eval(...) is evil[tm]. In most cases you will not need but
      avoid this method. Use the collection properties instead.


      PointedEars

      Comment

      • Lee

        #4
        Re: eval() not working

        Bill said:[color=blue]
        >
        >Hi
        >
        >I have a form called 'store' with many fields, and I can update the 'name'
        >field ok like this
        >
        >document.store .name.value = n; //this works fine
        >
        >but I want a function to update any field, so would like to do something
        >like this:
        >
        >function setStoreValue(f , v) {
        > eval ("document.stor e." + f + ".value") = v;
        >}
        >
        >but when I do this, nothing happens - no errors, nothing.
        >
        >Even this doesn't work:
        >
        >function setStoreNameVal ue(n) {
        > eval("document. store.name.valu e = " + n);
        >}
        >
        >
        >This eval function has me beat. Is this the only way to set this up?[/color]

        Don't use eval() for that. You've probably seen examples
        of doing it, but those examples are wrong. Use:

        function setStoreValue(f , v) {
        document.store. elements[f].value = v;
        }

        Comment

        • Bill

          #5
          Re: eval() not working

          Excellent... thanks!


          "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
          news:bmh800014e r@drn.newsguy.c om...[color=blue]
          > Bill said:[color=green]
          > >
          > >Hi
          > >
          > >I have a form called 'store' with many fields, and I can update the[/color][/color]
          'name'[color=blue][color=green]
          > >field ok like this
          > >
          > >document.store .name.value = n; //this works fine
          > >
          > >but I want a function to update any field, so would like to do something
          > >like this:
          > >
          > >function setStoreValue(f , v) {
          > > eval ("document.stor e." + f + ".value") = v;
          > >}
          > >
          > >but when I do this, nothing happens - no errors, nothing.
          > >
          > >Even this doesn't work:
          > >
          > >function setStoreNameVal ue(n) {
          > > eval("document. store.name.valu e = " + n);
          > >}
          > >
          > >
          > >This eval function has me beat. Is this the only way to set this up?[/color]
          >
          > Don't use eval() for that. You've probably seen examples
          > of doing it, but those examples are wrong. Use:
          >
          > function setStoreValue(f , v) {
          > document.store. elements[f].value = v;
          > }
          >[/color]


          Comment

          • Bill

            #6
            Re: eval() not working

            Cheers!



            "Martin Honnen" <mahotrash@yaho o.de> wrote in message
            news:3f8c2383$1 @olaf.komtel.ne t...[color=blue]
            >
            >
            > Bill wrote:
            >[color=green]
            > > Hi
            > >
            > > I have a form called 'store' with many fields, and I can update the[/color][/color]
            'name'[color=blue][color=green]
            > > field ok like this
            > >
            > > document.store. name.value = n; //this works fine
            > >
            > > but I want a function to update any field, so would like to do something
            > > like this:
            > >
            > > function setStoreValue(f , v) {
            > > eval ("document.stor e." + f + ".value") = v;
            > > }[/color]
            >
            > Forget about eval, JavaScript 1.x allows you to use any expression to
            > access the property of an object if you use square brackets:
            > document.store[f].value = v;
            >
            > --
            >
            > Martin Honnen
            > http://JavaScript.FAQTs.com/
            >[/color]


            Comment

            • Bill

              #7
              Re: eval() not working

              Consider eval forgotten. Thanks.


              "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
              news:3F8C23FD.5 090202@PointedE ars.de...[color=blue]
              > Bill wrote:
              >[color=green]
              > > [...]
              > > but I want a function to update any field, so would like to do something
              > > like this:
              > >
              > > function setStoreValue(f , v) {
              > > eval ("document.stor e." + f + ".value") = v;
              > > }
              > >
              > > but when I do this, nothing happens - no errors, nothing.[/color]
              >
              > 'cause that's fantasy syntax.
              >
              > Try this:
              >
              > function setStoreValue(f , v)
              > {
              > document.forms['store'].elements[f].value = v;
              > }
              >[color=green]
              > > This eval function has me beat. Is this the only way to set this up?[/color]
              >
              > eval(...) is evil[tm]. In most cases you will not need but
              > avoid this method. Use the collection properties instead.
              >
              >
              > PointedEars
              >[/color]


              Comment

              Working...