By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

eval problem

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.