eval problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pauljh@hushmail.com

    eval problem

    Hi All,
    I'm running some javascript over a server side generated web page
    and have multiple generated empty select statements, that I want to
    populate when the page is loaded. As HTML doesn't do arrays each
    select is individually named withe MySelecti where i is an incremental
    from 1.
    I know all my variables are correct (i, OptionsCount) and my arrays
    of MyValues and MyDescription's exist for multiple enteries and if I
    bring out an example of what I thought each line would eval too( say
    document.MyForm .MySelect1.opti ons[1]=new
    Option('Value1' ,'Description1' ) the line works fine, for the life of me
    (i'm sure I'm missing something obvious) the eval line won't eval..

    for(i = 1; i<=row; i++) {
    for (j = 1; j <=OptionsCoun t; j++) {
    eval="document. MyForm.MySelect "+i+".optio ns["+j+"]=new
    Option('"+MyVal ues[j]+"', '"+MyDescriptio n[j]+"')";
    eval(eval);
    }
    }

    Any help much appreciated

  • David Dorward

    #2
    Re: eval problem

    Pauljh@hushmail .com wrote:
    I know all my variables are correct (i, OptionsCount) and my arrays
    of MyValues and MyDescription's exist for multiple enteries and if I
    bring out an example of what I thought each line would eval too( say
    document.MyForm .MySelect1.opti ons[1]=new
    Option('Value1' ,'Description1' ) the line works fine, for the life of me
    (i'm sure I'm missing something obvious) the eval line won't eval..
    Don't use eval for this (as a rule of thumb, don't use eval).

    document.forms['MyForm'].elements['MySelect' + i].options[1]

    Comment

    • Richard Cornford

      #3
      Re: eval problem

      Pauljh@hushmail .com wrote:
      <snip>
      (i'm sure I'm missing something obvious) the eval line won't eval..
      >
      >for(i = 1; i<=row; i++) {
      for (j = 1; j <=OptionsCoun t; j++) {
      eval="document. MyForm.MySelect "+i+".optio ns["+j+"]=new
      This is assigning a string value to a variable (global or local) called
      'eval'. If that is a global variable then it may replace the gloabl -
      eval - function, or not.
      Option('"+MyVal ues[j]+"', '"+MyDescriptio n[j]+"')";
      eval(eval);
      If the - eval - to which you assigned the stirng is a local variable
      then that local - eval - will be making the global eval as it will come
      first on the scope chain, thus you would be attempting to call a stirng
      here. If the - eval - which the string value was assigned to was the
      global one then again you may be attempting to call a string, or if the
      global - eval - function did not allow itself to be replaced then it is
      the global - eval - function that becomes the argument to your - eval -
      call. You just cannot use the same Identifier as the name of a function
      you call ant to pass it an argument at the same time, it will not work.

      However, don't use - eval - for this at all, use bracket notation
      property accessors instead:-

      <URL: http://www.jibbering.com/faq/faq_not..._brackets.html >

      document.forms['MyForm'].elements['MySelect'+i].options[j] =
      new Option(MyValues[j], MyDescription[j]);

      Richard.

      Comment

      • Pauljh

        #4
        Re: eval problem

        Richard Cornford wrote:
        ><snip>
        If the - eval - to which you assigned the stirng is a local variable
        then that local - eval - will be making the global eval as it will come
        first on the scope chain, thus you would be attempting to call a stirng
        here. If the - eval - which the string value was assigned to was the
        global one then again you may be attempting to call a string, or if the
        global - eval - function did not allow itself to be replaced then it is
        the global - eval - function that becomes the argument to your - eval -
        call. You just cannot use the same Identifier as the name of a function
        you call ant to pass it an argument at the same time, it will not work.
        I think I know what your saying but I wouldn't want to repeat it.
        However, don't use - eval - for this at all, use bracket notation
        property accessors instead:-
        >
        <URL: http://www.jibbering.com/faq/faq_not..._brackets.html >
        >
        document.forms['MyForm'].elements['MySelect'+i].options[j] =
        new Option(MyValues[j], MyDescription[j]);
        >
        Richard.
        My days of become a good javascript programmer seam a long way off,
        thanks for the help Richard, worked a treat.

        Comment

        Working...