EVAL ( ) of javascript

hi experts

I am using eval function of javascript to evaluate some statements
i am not sure if i am going in right direction

i have this function in javascript


var state="state";
var region="region";
var county="county";
var municipality="municipality";
function fieldMapper(stateName,regionName,countyName,muniName)
{
alert("333");

state=stateName;
alert(stateName);
region=regionName;
county=countyName;
municipality=muniName;
}



This is statement before using eval


document.forms[0].state.options["document.forms[0].state.selectedIndex"].value;
BUT i want to evaluate the values of document.forms[0]....because i want stateName and not state 9from above function fieldmapper().

so after using eval i wrote something like this
 eval("document.forms[0]."+state+".options[")eval("document.forms[0]."+state+".selectedIndex")].value;

but it gives error.
any  help appreciated
thanks,
J
LVL 10
jaggernatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
What about:

var mySelect = document.forms[0].elements[state];
var myState   = mySelect.options[mySelect.selectedIndex];

-r-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaggernatAuthor Commented:
so dont i have to use eval()
0
RoonaanCommented:
no, you can access form elements through the elements array. You can even use:
document.forms[0].elements["state" + stateNumber]

-r-
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ppedregalCommented:
If you want to use the eval function the code would look like this:

var stateValue = eval("document.forms[0]."+state+".options[document.forms[0]."+state+".selectedIndex]").value;

or

var stateElement = eval("document.forms[0]."+state);
var stateValue = state.options[state.selectedIndex].value;
0
ppedregalCommented:
Srry last line in last post contains a bug, the correct code would be this one for the second alternative using "eval"

var stateElement = eval("document.forms[0]."+state);
var stateValue = stateElement.options[stateElement.selectedIndex].value;
0
jaggernatAuthor Commented:
var stateElement = eval("document.forms[0]."+state);
 var stateValue = stateElement.options[stateElement.selectedIndex].value;

why do i have to use var in both the statements
cant i just use

 stateElement = eval("document.forms[0]."+state);
 stateValue = stateElement.options[stateElement.selectedIndex].value;



0
RoonaanCommented:
You can, but it is cleaner to add "var" when you define variables.

-r-
0
ppedregalCommented:
If you use "var" you are creating local variables for the scope of the function
If you don't use "var" you are creating global variables.

Using "var" makes your code cleaner as said before and more efficient because the use of globals could cause performance problems.

There're situations however where you can't skip the use of globals, in that case its recommended that you declare them using "var" outside the scope of any function.

as tip always use "var" when declaring variables.

Examples:
<script>
var a = "a.foo";                  // GLOBAL SCOPE
a1 = "a1.foo";                    // GLOBAL SCOPE
function myFunc(){
  var b = "b.foo";                // FUNCTION SCOPE
  b1 = "b1.foo";                  // GLOBAL SCOPE
}
myFunc();
alert(a);    // -> a.foo
alert(a1);  // -> a1.foo
alert(b);    // -> Error b its not defined
alert(b1);  // -> b1.foo
</script>
0
jaggernatAuthor Commented:
thanks , it works now. One general question. how do i debug javascript errors. right now the browser is giving me error messages, but the error messages are not specific at all, Is there any way i can specifically know where the error is

thanks
J
0
RoonaanCommented:
I use firefox to do that, because the javascript console in firefox provides more details. Might be that opera and netscape have something similar, but wouldn't know.

-r-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.