eval() question

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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?
 
J

Janwillem Borleffs

Christopher said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top