Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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