Basic HTML version of Foils prepared 23 June 1997

Foil 37 The eval Function

From JavaScript Tutorial for Certificate Course UC Web applications Certificate -- Summer 97. by Geoffrey C. Fox, Tom Scavo

eval method
eval is an important function that lets you evaluate JavaScript code on-the-fly
For instance, document.forms[0].myName.value is the value of a form element:
<INPUT TYPE="text" NAME="myName" VALUE="value" onChange="handle(this.name)">
this.name holds myName as an ASCII string:
var x = document.forms[0].myName.value; // No!
var s = "document.forms[0]." + myName + ".value";
var x = eval(s); // Yes!
That is, eval applies the JavaScript interpreter to its argument
This can be used to copy information between forms as in
eval("document.forms[1]." + myName + ".defaultvalue")
= eval("document.forms[0]." + myName + ".value")
For a long time, eval didn't work right, so check your browser!



Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Apr 1 1998