eval() question

Discussion in 'Javascript' started by Christopher Benson-Manica, Jun 14, 2005.

  1. I've got the following complete web page...

    <html><head>
    <script>
    var allElements={};
    function addElements() {
    for( var idx=0; idx < addElements.arguments.length; idx++ ) {
    allElements[addElements.arguments[idx]]=0;
    }
    }
    function initializeElements() {
    for( elem in allElements ) {
    eval( elem+"=document.getElementById( '"+elem+"' );" );
    }
    }
    addElements( 'foo' );
    function ready() {
    initializeElements();
    }
    </script></head>
    <body onload="ready()">
    <input type="text" id="foo" size="1">
    </body></html>

    I'm basically trying to make declaring variables to store document
    nodes easier. This all works, except that for some reason the eval()
    says "Object doesn't support this property or method". The eval() is
    supposed to introduce a variable at global scope named elem - what is
    the problem?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jun 14, 2005
    #1
    1. Advertising

  2. Christopher Benson-Manica wrote:
    > I'm basically trying to make declaring variables to store document
    > nodes easier. This all works, except that for some reason the eval()
    > says "Object doesn't support this property or method". The eval() is
    > supposed to introduce a variable at global scope named elem - what is
    > the problem?


    Forget about using eval(), you can do without:

    function initializeElements() {
    for (elem in allElements) {
    window['elem'] = document.getElementById(elem);
    }
    }


    JW
    Janwillem Borleffs, Jun 14, 2005
    #2
    1. Advertising

  3. Janwillem Borleffs <> wrote:

    > Forget about using eval(), you can do without:


    > window['elem'] = document.getElementById(elem);


    Thank you!

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jun 15, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,342
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    517
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,397
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    121
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    145
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page