• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 698
  • Last Modified:

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
0
jaggernat
Asked:
jaggernat
  • 4
  • 3
  • 3
2 Solutions
 
RoonaanCommented:
What about:

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

-r-
0
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now