Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown menu in a form & eval() problem

Status
Not open for further replies.

Draug

Programmer
Apr 18, 2001
77
CA
Hi,

I have a function that creates and fills dropdown boxes. The function used to work fine when it was not embedded in a form. However, when I moved it into the form, I can no longer add my dynamically created OPTIONs.

I have a select statement making the dropdown box named locationCombo. This is what I had when the dropdown box was not in the form:

var theName = "locationCombo"
for (var j=0; j<currentNode.childNodes.length; j++)
{
var oOption = document.createElement(&quot;OPTION&quot;);
oOption.text = (currentNode.childNodes.item(j).text);
oOption.value = j;
(eval(theName)).options.add(oOption);
}

It is the last line that is giving me the grief. If I have to reference the form, I must change the last line to:

document.mainMenu.locationCombo.options.add(oOption);

However, I do not wish to have the 'locationCombo' hardcoded into the statement. I wish to use eval so that the names of the dropdown can be dynamic and this function can be generic. But, when I use the following I get an 'expected identifier' error.

document.mainMenu.(eval(theName)).options.add(oOption);

Why does eval seem to work outside of the form, but not inside the form? Is there anything else I can do to not have the names of boxes hardcoded?

Any help will be much appreciatted. Thanks is advance,
Draug
 
Hi Draug,

Why don't you look in the FAQ's, someone has posted one on creating option element ...

Hope this helps ...
 
SeAl,

I did read the FAQ article that Iza wrote which deals with OPTION creating. However, I dont think that my problem is in the creation of the OPTION since it works fine when it is not in a form, or when I hardcode the form name in the 'add option' statement.

Rather, I think my problem is in the use of the eval() function. What I really need to know is how to use eval() in my situation. Or if you can't use it like I want to, is there something else I could use?
 
For anyone that reads this post, I figured out why eval was not working within the form. I thought I would post the solution for anyone that was stuck like I was.

This is how eval() should be used in a given form, in this case it is called 'mainMenu'. The variable theName is the name of the select box:

eval(&quot;document.mainMenu.&quot; +theName+ &quot;.options.add(oOption)&quot;)

Hope this helps someone :)

 
The problem is not with the eval() function.
You just do not have dynamic access to listboxes on a form.
Place the listboxes outside of your <form></form> tags and it will work in IE and Opera, not in NN.
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top