472,135 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 software developers and data experts.

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.options[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 <=OptionsCount; j++) {
eval="document.MyForm.MySelect"+i+".options["+j+"]=new
Option('"+MyValues[j]+"', '"+MyDescription[j]+"')";
eval(eval);
}
}

Any help much appreciated

Aug 30 '06 #1
3 1803
Pa****@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.options[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]

Aug 30 '06 #2
Pa****@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 <=OptionsCount; j++) {
eval="document.MyForm.MySelect"+i+".options["+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('"+MyValues[j]+"', '"+MyDescription[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.

Aug 30 '06 #3
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.

Aug 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by Stuart | last post: by
7 posts views Thread by Reply Via Newsgroup | last post: by
6 posts views Thread by Roebie | last post: by
18 posts views Thread by Joe Fallon | last post: by
8 posts views Thread by santel_helvis | last post: by
5 posts views Thread by iulian.ilea | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.