Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Web
Expertise: Intermediate
Apr 28, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Call Functions by Name

JavaScript has a powerful mechanism to create and execute code on the fly using the eval() statement. This statement takes a string as a parameter. This string is then run just like the static <SCRIPT> code on the page. Because you can put any set of JavaScript statements into this string and have them execute you can also use this mechanism to call an arbitrary function--just like you would in static code. For example, this code calls a function based on the value a user enters into an text <INPUT> control:
function go() {
	alert("This is the GO function");

function stop() {
	alert("This is the STOP function");
// -->

Function Name: <INPUT TYPE="TEXT" NAME="funcname"
VALUE="go"> (try "go" or "stop")<BR><BR>
<INPUT TYPE="BUTTON" VALUE="Click here to display alert"
onClick="eval(this.form.funcname.value + '()')">

Boris Feldman
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date